使用 BaseAdapter 到 Listview.Adapter 的 JSON 内容对象

如何使用适配器将 json 对象结果放入列表视图中?


对象类


 public class ScVsrList

    {

        public int vnd { get; set; }


        public string drref { get; set; }

        public int dpt { get; set; }


        public string dname { get; set; }


        public int sdp { get; set; }

        public int cls { get; set; }

        public string cname { get; set; }



        public int ctn { get; set; }


        public List<ScDrList> drList2 { get; set; }



    }


    public class ScDrList

    {

        public int vnd2 { get; set; }


        public string drref { get; set; }



    }

   public  class StoreConsignorVsrObject

    {

        public string status { get; set; }


        public string env { get; set; }

        public string vsr { get; set; }

        public string type { get; set; }


        public List<ScVsrList> drList { get; set; }



    }



*我的代码使用 XAMARIN ANDROID 的 REST 客户端*


var client = new RestClient("http://10.121.4.72:10010/web/services/getVSRdt");

                var request = new RestRequest(Method.POST);


                request.AddHeader("Content-type", "application/json");


                var requestObject = new VsrRequestObject

                {

                    env = "DSP",

                    vsr = GlobalVariable.VsrNumber,

                    token = "967F058F023DA12798F2D41CDC2F2A5C6D4A6F5D40069A80V3S98R9RFPDT"


                };


                request.AddJsonBody(requestObject);

                var response = client.Execute(request);

                var content = response.Content;


                StoreConsignorVsrObject item = JsonConvert.DeserializeObject<StoreConsignorVsrObject>(content);


                if (item != null)

                {

                    LayoutInflater inflate = LayoutInflater.From(this);

                    View view = inflate.Inflate(Resource.Layout.activity_storeconsignorvsrmonitoring, null);

                    alertBuilder = new Android.Support.V7.App.AlertDialog.Builder(this);

                    alertBuilder.SetView(view);


                    contentDialog = alertBuilder.Create();


湖上湖
浏览 57回答 1
1回答

GCT1015

一个问题是items您StoreConsignorDetailAdapter应该列出的不是单个对象。因此您可以定义一个列表变量:&nbsp;private List<ScVsrList> drList;你可以这样做:public class StoreConsignorDetailAdapter: BaseAdapter<ScVsrList>{&nbsp; &nbsp; private StoreConsignorVsrObject item;&nbsp; &nbsp; AppCompatActivity activity;&nbsp; &nbsp; private List<ScVsrList> drList;&nbsp; // defile variable drList&nbsp; &nbsp; public StoreConsignorDetailAdapter(AppCompatActivity activity, StoreConsignorVsrObject item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.item = item;&nbsp; &nbsp; &nbsp; &nbsp; this.activity = activity;&nbsp; &nbsp; &nbsp; &nbsp; this.drList = item.drList; // assign value to drList&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public override ScVsrList this[int position]&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return drList[position];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public override int Count&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return drList.Count;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public override long GetItemId(int position)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return position;&nbsp; &nbsp; }&nbsp; &nbsp; public override View GetView(int position, View convertView, ViewGroup parent)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var item = drList[position];&nbsp; &nbsp; &nbsp; &nbsp; View view = convertView;&nbsp; &nbsp; &nbsp; &nbsp; if (view == null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view = activity.LayoutInflater.Inflate(Resource.Layout.adapter_storeconsignorvsrmonitoring, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //var detail = drList.FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewVendorNum).Text = item.vnd.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewVdrNumber).Text = item.drref.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewTotal).Text = item.ctn.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewMvdr).Text = item.drList2.Count.ToString();&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewDepartment).Text = item.dname;&nbsp; &nbsp; &nbsp; &nbsp; view.FindViewById<TextView>(Resource.Id.txtViewStatus).Text = "SHIPPED";&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP