2016-09-10 83 views
1

我嘗試搜索將JSONObject轉換爲HashMap,但大多數結果是針對Java而非Android。因此,如果您以前有過這方面的經驗,我希望有人可以分享。Android將JSONObject轉換爲HashMap並使用SimpleAdapter在ListView中顯示

listview_with_simpleAdapter_and_hashmap.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main4); 
    String[] food_id= new String[]{"1", "2", "3"}; 
    String[] food_name = new String[]{"apple", "orange", "banana"}; 
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>(); 

    for (int i = 0; i < 3; i++) { 
     HashMap<String, String> hm = new HashMap<String, String>(); 
     hm.put("ID", food_id[i]); 
     hm.put("Name", food_name[i]); 
     aList.add(hm); 
    } 

    String[] from = {"ID", "Name"}; 
    int[] to = {R.id.text_id, R.id.text_name}; 
    SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to); 
    ListView listView = (ListView) findViewById(R.id.listView); 
    listView.setAdapter(adapter); 

} 

此文件工作正常和簡單地在每行中顯示2列;

enter image description here

json.java

TextView mTxtDisplay; 
String url = "http://192.168.1.103/web_service/omg.php/"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mTxtDisplay = (TextView) findViewById(R.id.tv); 

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 
      mTxtDisplay.setText(response.toString()); 
     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      // TODO Auto-generated method stub 
     } 
    }); 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(jsObjRequest); 

192.168.1.103/web_service/omg.php/

{ 
"32":"Western Food", 
"35":"Japanese Food", 
"37":"Italian Food" 
} 

JSON是工作的罰款也是如此。格式與ListView數據 - > ID和名稱完全相同。

所以我的問題是如何將JSONObjectomg.php轉換爲listview_with_simpleAdapter_and_hashmap.java?我只需要一個簡單的例子。

+0

試試這個答案http://stackoverflow.com/questions/28221555/how-does -okhttp-get-json-string/31762661#31762661 –

+0

@codephillip這甚至不是Android - - – gosulove

回答

2

你可以做這樣的事情:

ListView listView = (ListView) findViewById(R.id.listView); 

// ... 

@Override 
public void onResponse(JSONObject response) { 
    List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

    try { 
     Iterator<String> iterator = response.keys(); 

     while (iterator.hasNext()) { 
      String key = iterator.next(); 
      String value = response.getString(key); 

      HashMap<String, String> map = new HashMap<>(); 
      map.put(KEY_ID, key); 
      map.put(KEY_NAME, value); 

      list.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    if(list.size() > 0) { 
     String[] from = {KEY_ID, KEY_NAME}; 
     int[] to = {R.id.text_id, R.id.text_name}; 

     SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, 
      R.layout.list_item, from, to); 

     listView.setAdapter(adapter); 
    } 
} 
0

試試這個代碼的JSONObject轉換爲HashMap的

Map<String, String> params = new HashMap<String, String>(); 
       try 
       { 

        Iterator<?> keys = jsonObject.keys(); 

        while (keys.hasNext()) 
        { 
         String key = (String) keys.next(); 
         String value = jsonObject.getString(key); 
         params.put(key, value); 

        } 


       } 
       catch (Exception xx) 
       { 
        xx.toString(); 
       } 
相關問題