Xamarin中的ListView使用RestApi

时间:2019-01-17 19:00:05

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

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

例如API包含此类数据:

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

错误屏幕截图: screenshot of error

将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>

2 个答案:

答案 0 :(得分:0)

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

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数据的列表视图

在列表视图中显示大数据,这是一个分页显示方法。获取API数据后,将其保存在本地CacheListData中。不要直接将其设置为listView.ItemsSource。您需要创建一个ItemListData来从CacheListData中添加数据。一次您想要显示的添加数据的数量。当listview滚动到底部时,然后显示 Add More Swipe方法重新加载下一页数据。

通常,解决方案是先将lagre数据缓存到本地,然后逐页获取数据以显示。Here is a solution link can refer to.

答案 1 :(得分:0)

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

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

您可以检查API documentation