0

Can we set an ArrayList containing HashMap into an ArrayAdapter?

I'm using:

ArrayList<HashMap<String,String>> items = new ArrayList<HashMap<String,String>>();
ArrayAdapter<ArrayList<HashMap<String, String>>> ad = new ArrayAdapter<ArrayList<HashMap<String,String>>>(this, android.R.layout.simple_list_item_1,items);

but this gives me an error saying:

The constructor ArrayAdapter>>(searchname, int, ArrayList>) is undefined

1

3 Answers 3

3

ArrayAdapter wants to know the type of the items inside the list, not the type of the list itself. Remove the enclosing ArrayList<> and it should work.

ArrayList<HashMap<String,String>> items =
        new ArrayList<HashMap<String,String>>();

ArrayAdapter<HashMap<String, String>> ad =
        new ArrayAdapter<HashMap<String,String>>(this,
            android.R.layout.simple_list_item_1,items);
Sign up to request clarification or add additional context in comments.

2 Comments

It print all the Items intead specific values inside hashmap.
@PawanSoni nothing in this example code or anywhere in this question prints anything, not entirely sure what you mean. But if you want use such an ArrayAdapter with a ListView or something similar you have to write code that uses the HashMap<String,String> items in the list in a way that is useful to you, that won't happen automatically.
0

have a look at this link Android Array Adapter with ArrayList and ListView not updating when the arraylist is changed.

basically you need to add your ArrayList into the ArrayAdapter through the add method.

Comments

0

You can convert the type HashMap<String,String>> to a class. For example, if the keys of the hashmap are countries and the values are lenguages, you can make a class called "Country" with this two variables (country,language). Then the AdapterClass have to extend the ArrayAdapter<Country>, in the getView method you can access both variables of each Country.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.