Home > Enterprise >  Get value from Label inside a listview xamarin
Get value from Label inside a listview xamarin

Time:11-02

I'm getting an id key from firebase realtime database, then i bind it into listview as label.text

What i want to do is to assign this idkey which is inside label.text as a string variable, and use this string variable inside XML.CS page in the future.

While trying to assign it as a string its not recognizing its x:name of the label

XML CODE

           <StackLayout>    
                <ListView x:Name="xListView" ItemsSource="{Binding xlist}" HasUnevenRows="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>

                            <StackLayout Orientation="Horizontal" Padding="5">
                                
                                <StackLayout HorizontalOptions="StartAndExpand">
                                   
                                    <Label x:Name="entryvalue" Text="{Binding Id}" FontSize="Medium"/>
                                    
                                </StackLayout>
                               
                            </StackLayout>

                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>

XML CS CODE

protected override async void OnAppearing()
        {

             var xlist = await GetAlles();          

             xListView.ItemsSource = xlist;

             //Tries to assing value to string variable

             string getentryvalue = entryvalue.Text;

             }

GetAll from database function inside XML CS file

public async Task<List<xModel>> GetAlles()
    {
       
        return (await firebaseClient.Child(nameof(xModel)).OnceAsync<xModel>()).Select(item => new xModel
        {
            
            Id = item.Key

        }).ToList();



    }

And the class

 public class xModel
{
    
    public string Id { get; set; }

}

CodePudding user response:

Like the comments are saying, you're over complicating this by a lot.

Your xlist variable already contains all of the id's.

To get a specific id, all you need to do is something like

var id = xlist[0].Id;

To go slightly deeper into an answer with what's not working for you, you cannot access a XAML element by x:name inside a ListView or CollectionView. Since the same template is used for each item, how is the code supposed to know which template you want when you say entryvalue?

  • Related