简体   繁体   English

[Xamarin]如何将列表中的结果绑定到Listview中?

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

So I am currently attempting to make an autocomplete feature for my Xamarin forms app. 因此,我目前正在尝试为Xamarin表单应用程序提供自动填充功能。 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: 该应用程序将从Google的Autocomplete API中获取输入,对其进行反序列化,然后将其传递到表中,然后该表将数据传递给listvbiew ...目前,我已经成功完成了以下工作:

  • Made the "Location" entry take an input and convert it into a Google Autocomplete API URL. 使“位置”条目接受输入并将其转换为Google自动完成API URL。

  • Send the request to Google's Autocomplete API, turning the request into a JSON package 将请求发送到Google的Autocomplete API,将请求转换为JSON包

  • Deserialized the JSON into a list named "results" 将JSON反序列化为名为“结果”的列表

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.stack.imgur.com/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: 从这里开始,我要做的就是知道如何获取在results/[0]/predictions/([0],[1],[2])找到的“描述”值results/[0]/predictions/([0],[1],[2])并将其绑定到我的列表视图中设置如下:

<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. 全部说完之后,它将使用结果列表中的三个描述值填充列表视图。

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] 我希望第一行具有来自结果/ [0] /预测/ [0]的描述值,第二行具有来自结果/ [0] /预测/ [1]的描述值,第三行具有结果/ [0] /预测/ [2]的描述值

then you only want results[0].predictions to be used as your ItemsSource 那么您只希望将results[0].predictions用作ItemsSource

ListViewUI.ItemsSource = results[0].predictions;

and your binding would just be to the description property 并且您的绑定将只是到description属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Android:如何在 ListView 中显示/绑定列表? - Xamarin Android: how to display/Bind List in ListView? Xamarin Forms-如何在ListView中显示/绑定列表? - Xamarin Forms - how to display/Bind List in ListView? 如何在 Xamarin.Forms 中的 ListView 中绑定列表 - How to bind list inside ListView in Xamarin.Forms 如何将数据从 MySQL 数据库绑定到 Xamarin ListView? - How do I bind data from MySQL database to Xamarin ListView? Xamarin - 如何将数据从 Rest API 绑定到 StackLayout 而不是 ListView - Xamarin - How to Bind Data From Rest API to a StackLayout NOT ListView 如何将字典列表中的值绑定到ListView? - How to bind values from a list of dictionaries to a ListView? 绑定列表视图Xamarin表单中的自定义项目列表 - bind custom list of items in a listview Xamarin forms 如何在Xamarin Forms中的另一个列表中获取列表并将其绑定到ListView中? - How to fetch list inside of another list and bind it inside ListView in Xamarin Forms? 如何在 Xamarin Forms 中添加将数组绑定到 ListView - How To Add Bind An Array To ListView in Xamarin Forms 如何绑定列表中的数据<class>到 Xamarin 中的 Picker 元素</class> - How to bind data from a List<class> to Picker element in Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM