1

我有一个如下所示的 JSON 对象

{"PAYLOAD":[{"pid":"4","title":"Kyamaiko Flats","long":"36.764880000","lat":"-1.342980800","volume":125,"value":"10000","active":"Y","contractor":"SS mehta and Sons","subContractor":"Kamau Njoro","cp":[{"contactPerson":"Njoroge","designation":"Architect"},{"contactPerson":"John","designation":"Quantity suveyor"}],"lastvisit":"2 months ago ","nextvisit":"10/12/2013 ""image_url":"http:\\www.someurl.net\images\avator.png"}]}

要获取值,我使用以下代码:

try{

                JSONArray  mot = json.getJSONArray("PAYLOAD");
                    for(int i=0;i<mot.length();i++){                        
                        HashMap<String, String> map = new HashMap<String, String>();        
                        JSONObject e = mot.getJSONObject(i);
                        map.put(KEY_NAME_ID, e.getString(KEY_NAME_ID));
                        map.put(KEY_NAME, e.getString(KEY_NAME));
                        map.put(KEY_LAT, e.getString(KEY_LAT));
                        map.put(KEY_LON, e.getString(KEY_LON));
                        map.put(KEY_VOLUME, e.getString(KEY_VOLUME));
                        map.put(KEY_VALUE, e.getString(KEY_VALUE));
                        map.put(KEY_ACCOUNT_STATUS, e.getString(KEY_ACCOUNT_STATUS));
                        map.put(KEY_CONTRACTOR, e.getString(KEY_CONTRACTOR));
                        map.put(KEY_SUB_CONTRACTOR, e.getString(KEY_SUB_CONTRACTOR));

                        JSONArray  cp = json.getJSONArray(KEY_CONTACT_PERSON);

                        for(int j=0;j<cp.length();j++)
                        {
                        HashMap<String, String> map_cp = new HashMap<String, String>();     
                        JSONObject f = cp.getJSONObject(j);
                        map_cp.put(KEY_CONTACT_PERSON_NAME, f.getString(KEY_CONTACT_PERSON_NAME));
                        map_cp.put(KEY_CONTACT_PERSON_DESIGNATION, f.getString(KEY_CONTACT_PERSON_DESIGNATION));
                         }
                        map.put(KEY_LAST_VISIT, e.getString(KEY_LAST_VISIT));
                        map.put(KEY_NEXT_VISIT, e.getString(KEY_NEXT_VISIT));
                        map.put(KEY_IMAGE_URL, e.getString(KEY_IMAGE_URL));
                        displaylist.add(map);
                    }       
              }catch(JSONException e)      {
                  Util.LogFailedVisits ("JSON Exception "+e.toString(),mDID);
              }

我想从第二个 JSONObject 传递值并将它们添加到 HashMap 并随后添加到 Arraylist,这样我就可以将它们传递给另一个 Fragment .. 这是最好的方法吗?或者我如何最好地将这个值传递给另一个片段?

4

2 回答 2

0

用于Fragment.setArguments()传递 Bundle 实例。在 Bundle 内,您可以放置​​一个Serializable对象(例如,您Collection的 of HashMap

于 2013-11-12T16:07:43.180 回答
0

您必须首先创建一个 Bundle,然后将可序列化的 hashmap 添加到 bundle 中,然后设置片段的参数,如下所示:

    Bundle args = new Bundle();
    args.putSerializable('key', 'value');

    Fragment fragment = new Fragment();
    fragment.setArguments(args);
于 2013-11-12T16:46:32.670 回答