【问题标题】:ListView in Xamarin using RestApiXamarin 中的 ListView 使用 Rest Api
【发布时间】:2019-01-17 19:00:04
【问题描述】:

我可以解决这个错误。我想显示大量 API 数据的ListView

例如 API 包含此类数据:

[{"id":"666","employee_name":"xyz","employee_salary":"123","employee_age":"23","profile_image":""}]

错误截图:

我将 JSON 转换为 c# 后制作的 Class.cs

 public class employees
    {

        public string id { get; set; }
        public string employee_name { get; set; }
        public string employee_salary { get; set; }
        public string employee_age { get; set; }
        public string profile_image { get; set; }

    }

这是 LoadData() 用于调用 API 的 XAML.cs 文件

public async void LoadData()
        {
            var content = "";
            HttpClient client = new HttpClient();
            var RestURL = "MY API";  
            client.BaseAddress = new Uri(RestURL);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync(RestURL);
            content = await response.Content.ReadAsStringAsync();
            var Items = JsonConvert.DeserializeObject<List<employees>>(content);
            ListView1.ItemsSource = Items;
        }

这是 Xamarin.Forms 的 XAML 文件:

<StackLayout BackgroundColor="White">
        <ListView x:Name="ListView1" RowHeight="60">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical" Padding="8,0,8,0">
                            <Label Text="{Binding id}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
                            <Label Text="{Binding employee_name}" TextColor="#000" LineBreakMode="TailTruncation" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

【问题讨论】:

  • 你的问题是什么?到目前为止,您尝试过什么?
  • 现有数百个教程和示例应用程序将向您展示如何从远程 API 加载数据并显示它。
  • 你有没有想过创建一个对象,并将这些json解析回一个列表,然后只设置为listview项源。
  • @Bruno Caceiro 请仔细阅读我上面列出的代码。
  • @Lee 你有任何关于你在说什么的代码示例,因为我是初学者,我不知道如何解析

标签: json api listview xamarin.forms cross-platform


【解决方案1】:

首先需要创建一个本地模型类:

public class TodoItem
{
    public string id { get; set; }

    public string employee_name { get; set; }

    public string employee_salary{ get; set; }

    public bool employee_age { get; set; }

    public bool profile_image { get; set; }
}

并且在RestService可以使用TodoItem

public List<TodoItem> Items { get; private set; }

public async Task<List<TodoItem>> RefreshDataAsync ()
{
   Items = new List<TodoItem> ();
 // RestUrl = http://developer.xamarin.com:8081/api/todoitems
   var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
   try {
       var response = await client.GetAsync (uri);
       if (response.IsSuccessStatusCode) {
           var content = await response.Content.ReadAsStringAsync ();
           Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
        }
    } catch (Exception ex) {
           Debug.WriteLine (@"ERROR {0}", ex.Message);
    }
    return Items;
}

最后你的列表视图itemsource 可以设置如下:

listView.ItemsSource = await RestService.RefreshDataAsync();

注意:这里有一个official sample,你可以参考一下。

我可以解决这个错误,我想显示大量 API 数据的列表视图

在listview中显示大数据,这里有一个分页显示方法。获取API数据后,保存在本地CacheListData。不能直接设置为listView.ItemsSource。而且你需要创建一个ItemListData 从 CacheListData 添加数据。添加数据的计数根据您曾经想要显示。当 listview 滚动到底部时,然后显示 Add More Swipe 方法以重新加载下一页数据。 p>

一般解决方法是先将lagre数据缓存到本地,然后一点一点的获取数据来展示。Here is a solution link can refer to.

【讨论】:

  • Jiang 请检查我上面列出的代码并告诉我我错在哪里以及我必须做什么。谢谢
  • 从图像显示>您需要检查api中的Json数据。这是您可以参考的解决方案。(stackoverflow.com/questions/49772027/…
【解决方案2】:

您使用了错误的端点,您应该使用这个端点来检索员工列表

 var RestURL = "http://dummy.restapiexample.com/api/v1/employees/";  

您可以查看API documentation

【讨论】:

    猜你喜欢
    • 2015-07-19
    • 2019-02-02
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2018-07-09
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多