绑定json数组响应xamarin android中listview列表中的行

时间:2016-03-25 16:36:08

标签: c# android json xamarin

我正在从列表的响应中绑定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;
    }
}

如何添加额外的第一行?

1 个答案:

答案 0 :(得分:0)

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;
    }