2015-05-05 79 views
0

您好,我從MySQL數據庫中檢索了一些數據,並將這些數據存儲在Hashmap中。我也創建了一個與Hashmap具有相同變量的類。我想遍歷Hashmap並存儲這些值,以便我可以將它們用於每個對象。任何想法如何做到這一點?遍歷Hashmap並將值存儲在變量中以供使用

public void updateJSONdata() { 

cafebartablesList = new ArrayList<HashMap<String, Integer>>(); 

JSONParser jParser = new JSONParser(); 

JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL); 

try { 

    cafebartables = json.getJSONArray(TAG_POSTS); 


    for (int i = 0; i < cafebartables.length(); i++) { 

     JSONObject c =cafebartables.getJSONObject(i); 

     int tableId = c.getInt(TAG_TABLE_ID); 
     int tableMarginLeft = c.getInt(TAG_MARGIN_LEFT); 
     int tableMarginTop = c.getInt(TAG_MARGIN_TOP); 
     int isFree = c.getInt(TAG_ISFREE); 

     map = new HashMap<String, Integer>(); 

     map.put(TAG_TABLE_ID, tableId); 
     map.put(TAG_MARGIN_LEFT, tableMarginLeft); 
     map.put(TAG_MARGIN_TOP, tableMarginTop); 
     map.put(TAG_ISFREE, isFree); 

     cafebartablesList.add(map); 


    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 
} 

public void printMap() { 
LoadMap ld = new LoadMap(); 

ld.execute(); 

} 

public class LoadMap extends AsyncTask<Void, Void, Boolean> { 

@Override 
protected Boolean doInBackground(Void... arg0) { 
    updateJSONdata(); 
    return null; 
} 

@Override 
protected void onPostExecute(Boolean result) { 
    super.onPostExecute(result); 

} 
} 
} 
+0

根據你的代碼,你只存儲單個(最後一個)數據,因爲你在hashmap中使用單個鍵。 –

+0

任何recomendations如何使它工作@Anjall Tripathi? – Vasilis13

回答

-1

爲什麼你想這樣做?你讓你的生活變得艱難。每個JSONObject後面都有一個LinkedHashMap,所以只需使用JSONObject就可以了,不需要任何轉換。即,改爲使用ArrayList<JSONObject>

+0

我沒有倒下你。相反,你的答案非常有用!非常感謝你!! – Vasilis13

相關問題