如何将嵌套 JSON 数据加载到 ExpendableListView
How to Load Nested JSON data into an ExpendableListView
这次我在我的应用程序中试验 ExpendableListView(Xamarin.Android)
我遵循了本网站上的教程:http://www.codeproject.com/Articles/677206/MonoAndroid-Writing-ExpandableListView-amd
我设法让示例在我自己的项目中运行,但现在我正在尝试将我的 json 数据加载到可扩展列表视图中。我的 json 嵌套了 objects,我不知道如何将这个嵌套数据加载到可扩展列表视图中。
我想要实现的是将 startTime(仅日期)放入 header 并将 chargingPointID 和时间中的所有其余部分放入子视图中。
JSON
[
{
"_id": "56e951fc2c559594037ceb7c",
"endTime": "2016-03-20T14:00:00.287Z",
"startTime": "2016-03-20T12:00:00.287Z",
"chargingPointID": {
"_id": "56e1815b78ac6b100f5de0d7",
"price": 5.55,
"type": "TypeBart",
"address": "Van Vaerenberghstraat 11, 2600 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:30:52.034Z",
"created_at": "2016-03-16T12:30:52.034Z"
},
{
"_id": "56e952062c559594037ceb7d",
"endTime": "2016-03-20T18:00:00.287Z",
"startTime": "2016-03-20T17:00:00.287Z",
"chargingPointID": {
"_id": "56e1815b78ac6b100f5de0d7",
"price": 5.55,
"type": "TypeBart",
"address": "Van Vaerenberghstraat 11, 2600 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:31:02.698Z",
"created_at": "2016-03-16T12:31:02.698Z"
},
{
"_id": "56e952182c559594037ceb7e",
"endTime": "2016-03-21T18:00:00.287Z",
"startTime": "2016-03-21T17:00:00.287Z",
"chargingPointID": {
"_id": "56e181d078ac6b100f5de0d8",
"price": 17.99,
"type": "TypeNG",
"address": "Osystraat 59, 2060 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:31:20.655Z",
"created_at": "2016-03-16T12:31:20.655Z"
}
]
ExpendListAdapter.cs
public class ExpendListAdapter: BaseExpandableListAdapter
{
Dictionary<string, List<string> > _dictGroup = null;
List<string> _lstGroupID = null;
Activity _activity;
public ExpendListAdapter (Activity activity,
Dictionary<string, List<string> > dictGroup)
{
_dictGroup = dictGroup;
_activity = activity;
_lstGroupID = dictGroup.Keys.ToList ();
}
#region implemented abstract members of BaseExpandableListAdapter
public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
{
return _dictGroup [_lstGroupID [groupPosition]] [childPosition];
}
public override long GetChildId (int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount (int groupPosition)
{
return _dictGroup [_lstGroupID [groupPosition]].Count;
}
public override View GetChildView (int groupPosition,
int childPosition,
bool isLastChild,
View convertView,
ViewGroup parent)
{
var item = _dictGroup [_lstGroupID [groupPosition]] [childPosition];
if (convertView == null)
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.ListControl_ChildItem, null);
var textBox = convertView.FindViewById<TextView> (Resource.Id.txtSmall);
textBox.SetText (item, TextView.BufferType.Normal);
return convertView;
}
public override Java.Lang.Object GetGroup (int groupPosition)
{
return _lstGroupID [groupPosition];
}
public override long GetGroupId (int groupPosition)
{
return groupPosition;
}
public override View GetGroupView (int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var item = _lstGroupID [groupPosition];
if (convertView == null)
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.ListControl_Group, null);
var textBox = convertView.FindViewById<TextView> (Resource.Id.txtLarge);
textBox.SetText (item, TextView.BufferType.Normal);
return convertView;
}
public override bool IsChildSelectable (int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount {
get {
return _dictGroup.Count;
}
}
public override bool HasStableIds {
get {
return true;
}
}
#endregion
}
MyReservationActivity.cs
public class MyReservationsActivity : Activity
{
Button buttonMyHome;
Dictionary<string, List<string> > dictGroup = new Dictionary<string, List<string> > ();
List<string> lstKeys = new List<string> ();
List<MyReservationClass> result = new List<MyReservationClass> ();
protected async override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.MyReservationsLayout);
CreateExpendableListData ();
var ctlExListBox = FindViewById<ExpandableListView> (Resource.Id.ctlExListBox);
ctlExListBox.SetAdapter (new ExpendListAdapter (this, dictGroup));
buttonMyHome = FindViewById<Button> (Resource.Id.btn_home);
try {
string pathUrl = "reservations/myReservations/"; //locatie van laadpalen
string currentToken = Settings.Token; //steek token in variabele
string currentUserID = Settings.User_ID;
System.Diagnostics.Debug.WriteLine ("My token: " + currentToken);
var getResponse = await RequestClass.GetMyReservations (pathUrl, currentToken, currentUserID);//Doe een get request en steek response in variabele
System.Diagnostics.Debug.WriteLine ("My GETresponse: " + getResponse.ToString ());
result = JsonConvert.DeserializeObject<List<MyReservationClass>> (getResponse);
} catch (Exception ex) {
Console.WriteLine ("Check error: " + ex);
}
buttonMyHome.Click += ButtonMyHome_Click;
}
void ButtonMyHome_Click (object sender, EventArgs e)
{
StartActivity (typeof(HomeActivity));
Finish ();
}
void CreateExpendableListData ()
{
for (int i = 0; i < result.Count; i++) {
var lstChild = new List<string> ();
for (int j = 0; j < result [i].chargingPointID; j++) {
Console.WriteLine (j);
}
dictGroup.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString ()), lstChild);
}
lstKeys = new List<string> (dictGroup.Keys);
}
}
如果我这样做:for (int j = 0; j < result [i].chargingPointID; j++)
我会收到一条错误消息:
the operator ‘<’ cannot be applied to operands of type ‘int’ and ‘CharchingPointID’
我也试过这个:
for (int i = 0; i < result.Count; i++) {
var lstChild = new List<string> ();
for (int j = 0; j < result.Count; j++) {
lstChild.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString (), result [i].chargingPointID));
}
dictGroup.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString ()), lstChild);
}
如何循环遍历 嵌套 objects 获取值并将其加载到可扩展列表视图中?
有人可以帮我解释一下吗?
提前致谢!
解决了!
这很容易。 (不知道为什么我没有早点想出这个解决方案)。
刚刚添加了一个额外的列表来存储对象 charchingPointID 的项目。
然后 juist 遍历那个额外的列表,瞧! :)
void CreateExpendableListData ()
{
for (int i = 0; i < sorted.Count; i++) {
var lstChild = new List<string> ();
var test = new List<string> ();
test.Add ("Address: " + sorted [i].chargingPointID.address);
test.Add ("Type: " + sorted [i].chargingPointID.type);
test.Add ("Price: €" + sorted [i].chargingPointID.price.ToString ());
for (int j = 0; j < test.Count; j++) {
lstChild.Add (test [j]);
Console.WriteLine (test [j]);
}
Console.WriteLine ("TEST LOOP: ", sorted [i].chargingPointID.address);
dictGroup.Add (string.Format (Convert.ToDateTime (sorted [i].startTime).ToShortDateString ()), lstChild);
}
lstKeys = new List<string> (dictGroup.Keys);
}
这次我在我的应用程序中试验 ExpendableListView(Xamarin.Android)
我遵循了本网站上的教程:http://www.codeproject.com/Articles/677206/MonoAndroid-Writing-ExpandableListView-amd
我设法让示例在我自己的项目中运行,但现在我正在尝试将我的 json 数据加载到可扩展列表视图中。我的 json 嵌套了 objects,我不知道如何将这个嵌套数据加载到可扩展列表视图中。
我想要实现的是将 startTime(仅日期)放入 header 并将 chargingPointID 和时间中的所有其余部分放入子视图中。
JSON
[
{
"_id": "56e951fc2c559594037ceb7c",
"endTime": "2016-03-20T14:00:00.287Z",
"startTime": "2016-03-20T12:00:00.287Z",
"chargingPointID": {
"_id": "56e1815b78ac6b100f5de0d7",
"price": 5.55,
"type": "TypeBart",
"address": "Van Vaerenberghstraat 11, 2600 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:30:52.034Z",
"created_at": "2016-03-16T12:30:52.034Z"
},
{
"_id": "56e952062c559594037ceb7d",
"endTime": "2016-03-20T18:00:00.287Z",
"startTime": "2016-03-20T17:00:00.287Z",
"chargingPointID": {
"_id": "56e1815b78ac6b100f5de0d7",
"price": 5.55,
"type": "TypeBart",
"address": "Van Vaerenberghstraat 11, 2600 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:31:02.698Z",
"created_at": "2016-03-16T12:31:02.698Z"
},
{
"_id": "56e952182c559594037ceb7e",
"endTime": "2016-03-21T18:00:00.287Z",
"startTime": "2016-03-21T17:00:00.287Z",
"chargingPointID": {
"_id": "56e181d078ac6b100f5de0d8",
"price": 17.99,
"type": "TypeNG",
"address": "Osystraat 59, 2060 Antwerpen, Belgia"
},
"userID": "56d05d7a475fec041bc2cab5",
"__v": 0,
"modified_at": "2016-03-16T12:31:20.655Z",
"created_at": "2016-03-16T12:31:20.655Z"
}
]
ExpendListAdapter.cs
public class ExpendListAdapter: BaseExpandableListAdapter
{
Dictionary<string, List<string> > _dictGroup = null;
List<string> _lstGroupID = null;
Activity _activity;
public ExpendListAdapter (Activity activity,
Dictionary<string, List<string> > dictGroup)
{
_dictGroup = dictGroup;
_activity = activity;
_lstGroupID = dictGroup.Keys.ToList ();
}
#region implemented abstract members of BaseExpandableListAdapter
public override Java.Lang.Object GetChild (int groupPosition, int childPosition)
{
return _dictGroup [_lstGroupID [groupPosition]] [childPosition];
}
public override long GetChildId (int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount (int groupPosition)
{
return _dictGroup [_lstGroupID [groupPosition]].Count;
}
public override View GetChildView (int groupPosition,
int childPosition,
bool isLastChild,
View convertView,
ViewGroup parent)
{
var item = _dictGroup [_lstGroupID [groupPosition]] [childPosition];
if (convertView == null)
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.ListControl_ChildItem, null);
var textBox = convertView.FindViewById<TextView> (Resource.Id.txtSmall);
textBox.SetText (item, TextView.BufferType.Normal);
return convertView;
}
public override Java.Lang.Object GetGroup (int groupPosition)
{
return _lstGroupID [groupPosition];
}
public override long GetGroupId (int groupPosition)
{
return groupPosition;
}
public override View GetGroupView (int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
var item = _lstGroupID [groupPosition];
if (convertView == null)
convertView = _activity.LayoutInflater.Inflate (Resource.Layout.ListControl_Group, null);
var textBox = convertView.FindViewById<TextView> (Resource.Id.txtLarge);
textBox.SetText (item, TextView.BufferType.Normal);
return convertView;
}
public override bool IsChildSelectable (int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount {
get {
return _dictGroup.Count;
}
}
public override bool HasStableIds {
get {
return true;
}
}
#endregion
}
MyReservationActivity.cs
public class MyReservationsActivity : Activity
{
Button buttonMyHome;
Dictionary<string, List<string> > dictGroup = new Dictionary<string, List<string> > ();
List<string> lstKeys = new List<string> ();
List<MyReservationClass> result = new List<MyReservationClass> ();
protected async override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.MyReservationsLayout);
CreateExpendableListData ();
var ctlExListBox = FindViewById<ExpandableListView> (Resource.Id.ctlExListBox);
ctlExListBox.SetAdapter (new ExpendListAdapter (this, dictGroup));
buttonMyHome = FindViewById<Button> (Resource.Id.btn_home);
try {
string pathUrl = "reservations/myReservations/"; //locatie van laadpalen
string currentToken = Settings.Token; //steek token in variabele
string currentUserID = Settings.User_ID;
System.Diagnostics.Debug.WriteLine ("My token: " + currentToken);
var getResponse = await RequestClass.GetMyReservations (pathUrl, currentToken, currentUserID);//Doe een get request en steek response in variabele
System.Diagnostics.Debug.WriteLine ("My GETresponse: " + getResponse.ToString ());
result = JsonConvert.DeserializeObject<List<MyReservationClass>> (getResponse);
} catch (Exception ex) {
Console.WriteLine ("Check error: " + ex);
}
buttonMyHome.Click += ButtonMyHome_Click;
}
void ButtonMyHome_Click (object sender, EventArgs e)
{
StartActivity (typeof(HomeActivity));
Finish ();
}
void CreateExpendableListData ()
{
for (int i = 0; i < result.Count; i++) {
var lstChild = new List<string> ();
for (int j = 0; j < result [i].chargingPointID; j++) {
Console.WriteLine (j);
}
dictGroup.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString ()), lstChild);
}
lstKeys = new List<string> (dictGroup.Keys);
}
}
如果我这样做:for (int j = 0; j < result [i].chargingPointID; j++)
我会收到一条错误消息:
the operator ‘<’ cannot be applied to operands of type ‘int’ and ‘CharchingPointID’
我也试过这个:
for (int i = 0; i < result.Count; i++) {
var lstChild = new List<string> ();
for (int j = 0; j < result.Count; j++) {
lstChild.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString (), result [i].chargingPointID));
}
dictGroup.Add (string.Format (Convert.ToDateTime (result [i].startTime).ToShortDateString ()), lstChild);
}
如何循环遍历 嵌套 objects 获取值并将其加载到可扩展列表视图中?
有人可以帮我解释一下吗?
提前致谢!
解决了!
这很容易。 (不知道为什么我没有早点想出这个解决方案)。
刚刚添加了一个额外的列表来存储对象 charchingPointID 的项目。 然后 juist 遍历那个额外的列表,瞧! :)
void CreateExpendableListData ()
{
for (int i = 0; i < sorted.Count; i++) {
var lstChild = new List<string> ();
var test = new List<string> ();
test.Add ("Address: " + sorted [i].chargingPointID.address);
test.Add ("Type: " + sorted [i].chargingPointID.type);
test.Add ("Price: €" + sorted [i].chargingPointID.price.ToString ());
for (int j = 0; j < test.Count; j++) {
lstChild.Add (test [j]);
Console.WriteLine (test [j]);
}
Console.WriteLine ("TEST LOOP: ", sorted [i].chargingPointID.address);
dictGroup.Add (string.Format (Convert.ToDateTime (sorted [i].startTime).ToShortDateString ()), lstChild);
}
lstKeys = new List<string> (dictGroup.Keys);
}