cdunn2013
cdunn2013

Reputation: 214

[Xamarin]How to bind results from list into Listview?

So I am currently attempting to make an autocomplete feature for my Xamarin forms app. The App will take input from Google's Autocomplete API, deserialize it, and pass it into a table, which will then hand the data off to a listvbiew... Currently I have successfully done the following:

here is the majority of the code responsible for the above:

 protected async void GetInfo(){
        if (CrossConnectivity.Current.IsConnected){
            try{
                HttpClient webSource = new HttpClient();
                Activity_Indicator.IsRunning = true;
                var content = await webSource.GetStringAsync(url);
                string EditedJSON = "[" + content + "]";
                var DeserializedJSON = JsonConvert.DeserializeObject<List<PlacesAPI>>(EditedJSON);
                ObservableCollection < PlacesAPI > results = new ObservableCollection<PlacesAPI>(DeserializedJSON);
                ListViewUI.ItemsSource = results;


            }
            catch(Exception ey){
                Debug.WriteLine("" + ey);
            }
        }
    }

This gives me the following list: https://i.sstatic.net/yMU78.png

From here all that I am needing to do is know how to go about taking the "description" value found in results/[0]/predictions/([0],[1],[2]) and bind it into my listview which is set up as such:

<ListView x:Name="ListViewUI">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding ???}" TextColor="Black"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

when all is said and done, it will populate the listview with three description values from the results list.

Upvotes: 2

Views: 158

Answers (1)

Jason
Jason

Reputation: 89082

I want the first row to have the description value from results/[0]/predictions/[0], the second row to have the description value from results/[0]/predictions/[1], and the third row to have the description value from results/[0]/predictions/[2]

then you only want results[0].predictions to be used as your ItemsSource

ListViewUI.ItemsSource = results[0].predictions;

and your binding would just be to the description property

<Label Text="{Binding description}" TextColor="Black"/>

Upvotes: 3

Related Questions