使用HTTP请求获取数据Android Xamarin C#

使用HTTP请求获取数据Android Xamarin C#,c#,android,http,android-networking,C#,Android,Http,Android Networking,朋友们,这是我的第一个Android应用程序,我正在尝试从WEPAPI获取数据并显示在列表视图中。 现在,我将首先告诉我的步骤,我创建了一个类,这个类通过http请求获取数据 class WebRequests { public static List<Item> GetAllItems() { List<Item> lp = new List<Item>(); HttpClient client = new H

朋友们,这是我的第一个Android应用程序,我正在尝试从WEPAPI获取数据并显示在列表视图中。 现在,我将首先告诉我的步骤,我创建了一个类,这个类通过http请求获取数据

class WebRequests
{
    public static List<Item> GetAllItems()
    {
        List<Item> lp = new List<Item>();
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        try
        {
            HttpResponseMessage response = client.GetAsync("https://MyUrlToApi/api/myitems").Result;
            if (response.IsSuccessStatusCode)
            {
                lp = JsonConvert.DeserializeObject<Item[]>(response.Content.ReadAsStringAsync().Result).ToList();
            }
        }
        catch
        {

        }
        return lp;
    }
我的主要活动是:

[Activity(Label = "GetDataFromWebApiAndroid", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    ListView listView;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        var result = WebRequests.GetAllItems();

        listView = FindViewById<ListView>(Resource.Id.listAll);
        listView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, result);
        listView.ChoiceMode = ChoiceMode.None;
    }
}
[活动(Label=“GetDataFromWebApiAndroid”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
列表视图列表视图;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
var result=WebRequests.GetAllItems();
listView=findviewbyd(Resource.Id.listAll);
Adapter=newarrayadapter(这个,Android.Resource.Layout.SimpleListItem1,结果);
listView.ChoiceMode=ChoiceMode.None;
}
}
因此,当我运行应用程序时,我的列表视图显示:

但我的数据是:


谁能帮我解释一下我做错了什么。

我找到了解决方案,也许这会帮助新的Android开发者。特别感谢 安德烈代表正确的道路。 所以为了正确显示数据,我应该创建自定义适配器来绑定数据。 首先必须创建Item类,我称之为model:

public class Item
{
    public string Id { get; set; }
    public string Content { get; set; }

    public Item(string Id, string Content)
    {
        this.Id = Id;
        this.Content = Content;
    }
}
现在自定义适配器:

class ItemListAdapter : BaseAdapter<Item>
{
    Activity context;
    public List<Item> listItems { get; set; }
    public ItemListAdapter(Activity context, List<Item> Items) : base()
    {
        this.context = context;
        this.listItems = Items;
    }
    public override Item this[int position]
    {
        get
        {
            return this.listItems[position];
        }
    }

    public override int Count
    {
        get
        {
            return this.listItems.Count;
        }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        Item item = listItems[position];
        View view = convertView;
        if (convertView == null || !(convertView is LinearLayout))

            view = context.LayoutInflater.Inflate(Resource.Layout.Itemlayout, parent, false);
        TextView itemId = view.FindViewById(Resource.Id.textItemId) as TextView;
        TextView itemContent = view.FindViewById(Resource.Id.textItemContent) as TextView;

        itemId.SetText(item.Id, TextView.BufferType.Normal);
        itemContent.SetText(item.Content, TextView.BufferType.Normal);

        return view;

    }
}
class ItemListAdapter:BaseAdapter
{
活动语境;
公共列表列表项{get;set;}
公共ItemListAdapter(活动上下文,列表项):base()
{
this.context=上下文;
this.listItems=项目;
}
公共覆盖项此[int位置]
{
得到
{
返回此.listItems[位置];
}
}
公共覆盖整数计数
{
得到
{
返回this.listItems.Count;
}
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
项目=列表项目[位置];
视图=转换视图;
如果(convertView==null | |!(convertView为线性布局))
视图=context.LayoutInflater.Inflate(Resource.Layout.Itemlayout,父项,false);
TextView itemId=view.findviewbyd(Resource.Id.textItemId)作为TextView;
TextView itemContent=view.findviewbyd(Resource.Id.textItemContent)作为TextView;
itemId.SetText(item.Id,TextView.BufferType.Normal);
SetText(item.Content,TextView.BufferType.Normal);
返回视图;
}
}
最后在活动中进行更改:

[Activity(Label = "CustomAdapterAndroidApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    private ListView listItems;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        listItems = FindViewById<ListView>(Resource.Id.listViewItems);
        PopulateItemsList();
    }

    private void PopulateItemsList()
    {
        listItems.Adapter = new ItemListAdapter(
           this, new List<Item>
            {
                new Item("222", "First Sample"),
                new Item("111", "Второй пример"),
                new Item("333", "მესამე მანალითი")
            }); 

    }
[活动(Label=“CustomAdapterAndroidApp”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
私有列表视图列表项;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
listItems=FindViewById(Resource.Id.listViewItems);
PopulateItemsList();
}
私有void PopulateItemsList()
{
listItems.Adapter=新项目ListAdapter(
这是一份新的名单
{
新项目(“222”,“第一个样本”),
新的项目(“111”和“111”),
新项目(“333”მესამე მანალითი")
}); 
}
不要忘记创建特殊布局以显示每个项目:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<TextView
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textItemId" />
<TextView   
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textItemContent" />
</LinearLayout>

确定主布局中的位置列表。
再次感谢。

我从未使用过Xamarin或C#,但看屏幕截图和代码,似乎
ArrayAdapter
无法正确呈现给定的对象列表:
list
,这有点道理。例如:适配器如何知道列表中要显示什么?只有项的
Id
?或
内容ode>或者一起使用?我相信您需要的是一个自定义适配器。非常感谢,正如我看到的,我必须了解有关自定义适配器的更多信息-自定义适配器将绑定数据是吗?确切地说,自定义适配器用于将复杂数据绑定到列表视图。@AndyRes非常感谢,我想我很快就会找到解决方案。
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<TextView
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textItemId" />
<TextView   
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textItemContent" />
</LinearLayout>