如何将嵌套的JSON数据加载到ExpendableListView中

时间:2016-03-21 23:03:43

标签: c# json listview xamarin xamarin.android

这次我在我的应用中尝试使用 ExpendableListView (使用Xamarin.Android)

我按照本网站上的教程:http://www.codeproject.com/Articles/677206/MonoAndroid-Writing-ExpandableListView-amd

我设法让样本在我自己的项目中工作,但现在我正在尝试将我的json数据加载到消耗品列表视图中。我的json有嵌套对象,我不知道如何将这个嵌套数据加载到一个可消耗的列表视图中。

我想要实现的是将startTime(仅限日期)放入标题中,将所有其余内容放入chargingPointID和timeview中。

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++)

我将收到错误说明以下内容:

  

运算符'&lt;'不能应用于'int'和'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);
        }

如何循环遍历嵌套对象获取值并将其加载到消耗性列表视图中?

有人可以帮我解释一下吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

解决了!

这很简单。 (不知道为什么我之前没有提出这个解决方案。)

刚添加了一个额外的列表来存储对象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);
    }