Loop through listview to get labels inside a listview

patapatanponpatapatanpon Member ✭✭
edited July 31 in Xamarin.Forms

Hello everyone, I am a beginner in Xamarin so sorry if I sound dumb or anything. I want to access to the listview labels in the code behind using a loop? (not sure if it is possible but please do advice me on ways I should access the binding label in the listview in code behind) thank you!

        <ListView ItemsSource="{Binding FruitsList}"
              HasUnevenRows="True"
              SeparatorVisibility="None"            
              x:Name="FruitsList">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout x:Name="stack">
                            <Label Text="{Binding Name}" />
                            <Label Text="{Binding Price}" />
                            <Label Text="{Binding Quantity}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

xaml.cs
BindingContext = new Fruits();

I thought of using this but I'm not sure what to include inside the loop
foreach (var item in FruitsList.ItemsSource)
{
//Don't know what to include here
}

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @patapatanpon

    Can you explain what are you trying to achieve?
    Also, can you post your current code for Fruits and FruitsList?

  • patapatanponpatapatanpon Member ✭✭

    @JohnHardman

    FruitsList is an observablecollection which gets the data from the Fruits.cs (which contains the get set such as public string Name { get; set; })
    What I am trying to do is to get the FruitsList binding of Name, Price and Quantity and save it to database or something but I could not access to the label in a listview.
    For example, my listview shows
    Apple, $1, 1
    Banana, $1.50, 3
    Orange, $1, 4

    I want to save the 3 rows into my sqlite db. I am not good in explaining myself so let me know if you are still confuse on what I am doing. Thank you for your reply!

  • JohnHardmanJohnHardman GBUniversity mod

    @patapatanpon

    What you are describing sounds the wrong way around.

    Your code behind will typically include a View Model class, which in your case would contain an ObservableCollection.
    Your XAML View would then use data binding to populate the ListView from the View Model's state.
    Your XAML View might also use data binding to update the View Model with details of which item is selected at any time.

    If you want to retrieve the information that populates the ObservableCollection from a database, rather than hard-coding the values, that makes sense.
    Similarly, if you want to save the selected item in the database, that could also make sense.
    However, I am unclear as to why you would want to save all three rows from the ObservableCollection to a database. Even if you were populating the list via a user interface where the user specifies a fruit, price and quantity, and says Add, you would still only write one item at a time to an underlying database, not all three in one go.

  • lbk3lbk3 Member ✭✭

    Building on your foreach that you included at the bottom of your post, I'll make use of that since I'm assuming you want to implement it that way.
    List fruits = new List();
    foreach (var item in FruitsList.ItemsSource)
    {
    fruits.Add(item);
    }

    From this you can access each label's text by the index in the list. If you want to take this one step further and be able to access it in the View then you can assign it to a property instead. Code as above remains the same but switch out the local variable implementation of the list for something like:

    private List _fruits;
    public List Fruits
    {
    get => _fruits;
    set => SetProperty(ref _fruits, value);
    }

    Then you can do Fruits.Add(item); and that "Fruits" property becomes available to you as a bindable context in the XAML or as a value source in the code behind :smile:

  • patapatanponpatapatanpon Member ✭✭

    @JohnHardman
    Would it make more sense if I want to save the listview items in another Table in sqlite database? Or does it strange to save the 3 items in my listview

  • patapatanponpatapatanpon Member ✭✭

    @lbk3 Hi, thanks for your reply! But I am getting an error when I typed in List fruits = new List();
    Do I have to type in List fruits = new List(); or?

  • JohnHardmanJohnHardman GBUniversity mod

    @patapatanpon said:
    @JohnHardman
    Would it make more sense if I want to save the listview items in another Table in sqlite database? Or does it strange to save the 3 items in my listview

    I think it would help to know why you want to do these things. At the moment, what you are suggesting is not making sense to me. See my previous post.

  • lbk3lbk3 Member ✭✭

    @patapatanpon My post was just some pseudo-code I quickly threw together, it was never going to play nicely just ripped out straight from here. You need to specify the type of thing you're going to store in the list you've made;
    List< int >
    List< string >
    List < CustomObject >

  • NMackayNMackay GBInsider, University mod

    @patapatanpon said:
    @lbk3 Hi, thanks for your reply! But I am getting an error when I typed in List fruits = new List();
    Do I have to type in List fruits = new List(); or?

    Share a repo app, otherwise anyone trying to help you is doing it blindfolded as you aren't supplying enough detail.

Sign In or Register to comment.