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
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 aList, or use different keys.Answering the comment by giving examples:
Using
ArrayList/List:Using different keys:
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.