How to put the json object result into a list view using adapter?
Object class
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; }
}
*MY CODE USING REST CLIENT FROM XAMARIN ANDROID *
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();
BtnReturnDialog = FindViewById<Button>(Resource.Id.btnReturn);
listViewStoreConsignor = FindViewById<ListView>(Resource.Id.listViewStoreConsignor);
listViewStoreConsignor.Adapter = new StoreConsignorDetailAdapter(this, item);
}
else
{
DialogHelper.ShowAlertMessage(this, "Error Data Findings", "No Detail Found");
return;
}
StoreConsignorDetailAdapter
public class StoreConsignorDetailAdapter : BaseAdapter<StoreConsignorVsrObject>
{
private StoreConsignorVsrObject items;
AppCompatActivity activity;
public StoreConsignorDetailAdapter(AppCompatActivity activity, StoreConsignorVsrObject items)
{
this.items = items;
this.activity = activity;
}
public override StoreConsignorVsrObject this[int position]
{
get
{
return items[position];
}
}
public override int Count
{
get
{
return items.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = activity.LayoutInflater.Inflate(Resource.Layout.adapter_storeconsignorvsrmonitoring, null);
}
var detail = item.drList.FirstOrDefault();
view.FindViewById<TextView>(Resource.Id.txtViewVendorNum).Text = detail.vnd.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewVdrNumber).Text = detail.drref.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewTotal).Text = detail.ctn.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewMvdr).Text = detail.drList2.Count.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewDepartment).Text = detail.dname;
view.FindViewById<TextView>(Resource.Id.txtViewStatus).Text = "SHIPPED";
return view;
}
}
Im getting an error from my Adapter
Err 1: cannot apply indexing with to an expression of type 'object' from items[position]
public override StoreConsignorVsrObject this[int position]
{
get
{
return items[position];
}
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null)
{
view = activity.LayoutInflater.Inflate(Resource.Layout.adapter_storeconsignorvsrmonitoring, null);
}
var detail = item.drList.FirstOrDefault();
view.FindViewById<TextView>(Resource.Id.txtViewVendorNum).Text = detail.vnd.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewVdrNumber).Text = detail.drref.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewTotal).Text = detail.ctn.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewMvdr).Text = detail.drList2.Count.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewDepartment).Text = detail.dname;
view.FindViewById<TextView>(Resource.Id.txtViewStatus).Text = "SHIPPED";
return view;
}
ERR 2 Cannot Convert group method Count to a non delegate type 'int':
public override int Count
{
get
{
return items.Count;
}
}
I expect a clean output of my list view through my json outputs. My json is consists also of Json Array, so it's a nested Json.
One problem is the items
in your StoreConsignorDetailAdapter
should be list not a single Object.So you can define a list variable:
private List<ScVsrList> drList;
You can do like this:
public class StoreConsignorDetailAdapter: BaseAdapter<ScVsrList>
{
private StoreConsignorVsrObject item;
AppCompatActivity activity;
private List<ScVsrList> drList; // defile variable drList
public StoreConsignorDetailAdapter(AppCompatActivity activity, StoreConsignorVsrObject item)
{
this.item = item;
this.activity = activity;
this.drList = item.drList; // assign value to drList
}
public override ScVsrList this[int position]
{
get
{
return drList[position];
}
}
public override int Count
{
get
{
return drList.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = drList[position];
View view = convertView;
if (view == null)
{
view = activity.LayoutInflater.Inflate(Resource.Layout.adapter_storeconsignorvsrmonitoring, null);
}
//var detail = drList.FirstOrDefault();
view.FindViewById<TextView>(Resource.Id.txtViewVendorNum).Text = item.vnd.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewVdrNumber).Text = item.drref.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewTotal).Text = item.ctn.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewMvdr).Text = item.drList2.Count.ToString();
view.FindViewById<TextView>(Resource.Id.txtViewDepartment).Text = item.dname;
view.FindViewById<TextView>(Resource.Id.txtViewStatus).Text = "SHIPPED";
return view;
}
}