Reputation: 464
I can fetch the menucode from database i can print inside for loop means it prints all menucodes. if i print loop out side means it print only last value
my code
JSONArray json = jArray.getJSONArray("mainmenu");
for ( i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
map.put("itemcode", e.getString("menucode")); //here i print all values
}
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println(map.get("itemcode"));//here i print only last value(here i want print all values how can i do this)
// TODO Auto-generated method stub
Intent intent=new Intent(getApplicationContext(),FoodMenu.class);
startActivity(intent);
}
});
out side for loop also i want print all menucodes .. please help me
Upvotes: 0
Views: 713
Reputation: 137312
You are using a map, and you have the same key all the time ("itemcode"
), so each insertion overrides the previous insertion to the map, maybe you should just use a List
, or use different keys.
Answering the comment by giving examples:
Using ArrayList
/ List
:
List<String> codez = new ArrayList<String>();
for ( i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
codez.add(e.getString("menucode"));
}
Using different keys:
for ( i = 0; i < json.length(); i++) {
JSONObject e = json.getJSONObject(i);
map.put("itemcode" + i, e.getString("menucode"));
}
Last thing - you cannot have two identical keys in a map, it defeats its purpose, and it's useless. If you are not going to get the values with by the key, and if the key is not used, you shouldn't use a map in first place.
Upvotes: 1