简体   繁体   中英

Android HashMap to ListView

I am trying to list all the values of hash map like this:

item1 value1 item2 value2 . . .

To do this, i pass the hashmap to another activity, and on another activity; i do some stuff like this:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.orneksayisi);
        listV=(ListView)findViewById(R.id.listView1);
        ourMap=(HashMap<String, Integer>) getIntent().getSerializableExtra("ornekMap");
        String[] from = { "aktivite", "sayi" };
        int[] to = { android.R.id.text1, android.R.id.text2 };

        ArrayList<Map<String, String>> list = buildData();
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_1, from, to);
            listV.setAdapter(adapter);
    }

   public ArrayList<Map<String,String>> buildData(){
       ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();

       list.add(putData("durus",ourMap.get("durus").toString()));
       list.add(putData("kosma",ourMap.get("kosma").toString()));
       list.add(putData("yurume",ourMap.get("yurume").toString()));
       list.add(putData("dusme",ourMap.get("dusme").toString()));
       list.add(putData("inme",ourMap.get("inme").toString()));
       list.add(putData("cikma",ourMap.get("cikma").toString()));

       return list;
   }

   private HashMap<String, String> putData(String name, String value) {

        HashMap<String, String> item = new HashMap<String, String>();
        item.put("aktivite", name);
        item.put("sayi", value);
        return item;
      }

when i try this, it only shows "key" values not shows opposite value (one column).How can i display two values (key,value) in a row?

How can i display two values (key,value) in a row?

You need to use a layout that shows more than one entry, try using android.R.layout.simple_list_item_2 .

SimpleAdapter adapter = new SimpleAdapter(this, list,
        android.R.layout.simple_list_item_2, from, to);

(or write your own layout.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM