0

If I have a hashmap containing the following:

Hashmap contains (String, String)

How can I instantiate a custom adapter? The custom adapter should extend baseadapter. I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.

Instantiation should look something like this:

MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);   
setListAdapter(adapter)

I am lost here so code would be a big help.

THANKS graham

The following list is using as its datasource a string array called items.

public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items)); 

however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap

so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm

Anupam Saini
  • 2,431
  • 2
  • 23
  • 30
user803271
  • 115
  • 5
  • 16
  • I really don't understand what you are trying to do... If you want to show to the user a list of key+value pairs you have just to use the code in my answer. Try to see this [link](http://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview). – Ant4res Apr 04 '12 at 12:40

1 Answers1

1

I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter. For you example you can do:

HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
 for(int i=0; i<=10;i){      
      hm.put(i,("num"+i));
    }
    for (Entry<Integer, String> e : hm.entrySet()) {
        String newString=e.toString();
        elements.add(newString);
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
    list.setAdapter(adapter);
Ant4res
  • 1,217
  • 1
  • 18
  • 36
  • Hi, It looks better now but in my list I get 0=num1.... I want populate it with the values for the following hashmap hm.put ("one", "Apple"); hm.put("two", "Mango"); hm.put("three", "Grape"); the values and keys should be concented in elements thanks – user803271 Apr 04 '12 at 10:08
  • FIGURED IT OUT for(int i=0; i<=10;i++) { hm.put(1, "Apple"); hm.put(2, "Mango"); hm.put(3, "Grape"); hm.put(4, "Orange"); hm.put(5, "Peach"); } for (Entry e : hm.entrySet()) { String newString=e.toString(); elements.add(newString); } – user803271 Apr 04 '12 at 10:10
  • How do you want them to appear on the list? Only Apple, Mango and Grape? To do that you have only to change the `e.toString()` with `e.getValue().toString()`. – Ant4res Apr 04 '12 at 10:11
  • Hi, I see one more problem i have declared public ArrayList myList = new ArrayList(Arrays.asList(items)); and items is a string array. How can I update this to incluce hm.put(1, "Apple"); hm.put(2, "Mango"); hm.put(3, "Grape"); hm.put(4, "Orange"); hm.put(5, "Peach"); – user803271 Apr 04 '12 at 10:15
  • What is `myList` and what `items` contain? Could you please edit your first question with your code, otherwise I can't understand what you are trying to do. – Ant4res Apr 04 '12 at 10:24
  • updated the list, I want to stop using a String of items and instead use key+value pairs – user803271 Apr 04 '12 at 10:29
  • Edit your question with your code, so I can better understand what's your problem – Ant4res Apr 04 '12 at 10:48
  • The following list is using as its datasource a string array called items. public ArrayList myList = new ArrayList(Arrays.asList(items)); however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm – user803271 Apr 04 '12 at 11:15