在Xamarin android中将json数组响应绑定到列表视图中的列表行

舌头

我将响应中的json数据绑定到列表。

回复

[{
    "DDLValue":"home",
    "ID":2,
    "Msg":"Enough",
    "Status":2
}
  {
    "DDLValue":"palace",
    "ID":1,
    "Msg":"good",
    "Status":2
}]

我可以将上述json响应绑定到ListView,但我希望包含单词“ select”的第一行以及随后的所有行。

Mainactivity.cs

tableItems = new List<TableItem>();
var client = new RestClient("http://websites.net/");
var request = new RestRequest("Service/GetRegions", Method.POST);
request.RequestFormat = DataFormat.Json;
tableItems = client.Execute<List<TableItem>>(request).Data;

listView.Adapter = new HomeScreenAdapter(this, tableItems);
    protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
    {
                   string val;
        if (e.Position==0)
        { 
            region.Text = -select-;
        }
        else {


       var  t1 = tableItems[e.Position-1];
            val = t1.DDLValue;
            region.Text = val;
        }}

HomeScreenAdapter.cs

public class HomeScreenAdapter : BaseAdapter<TableItem> {
    List<TableItem> items;
    Activity context;
    public HomeScreenAdapter(Activity context, List<TableItem> items)
        : base()
    {

        this.context = context;
        this.items = items;
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override TableItem this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count+1; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];

        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.DDLValue;

        return view;
    }
}

如何添加额外的第一行?

杰森
public override View GetView(int position, View convertView, ViewGroup parent)
    {
        TableItem item;

        if (position == 0 {
          item = new TableItem { DDLValue = "Select" };
        } else {
          item = items[position - 1];
        }

        View view = convertView;

        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomView, null);
        view.FindViewById<TextView>(Resource.Id.Text1).Text = item.DDLValue;

        return view;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档