0

I use a custom list adapter and ArrayList for my ListView. This solution was good enough but now I need to use Map of ArrayLists, something like this:

TreeMap<String, ArrayList<ItemsModel>>

where ItemsModel is a Java bean. Earlier I used to populate this ArrayList it that way:

itemsDataArrayList.add(itemModel)

Now I faced some difficulties with Map interface. First, I don't know how to populate my new Map structure: I suppose this

mapInstance.put(itemModel.getItemName.toString(), itemsDataArrayList)

won't work because itemsDataArrayList is the list of elements, not a certain element.

Second, I'm not sure how to properly declare this map instance in my ItemsAdapter class. When I was using just ArrayList it was very simple. Some examples would be very helpful.

2
  • I'd like to arrange list elements by their keys (string elements from map) so that entries with the same keys will be laid out simultaneously. Commented May 5, 2011 at 14:00
  • You could use a custom Comparator to sort the list by that Key if you just want to keep them ordered. If you want them displayed differently, based on their Key, then the answer from Gaunt Face down there looks pretty good. Commented May 5, 2011 at 14:11

2 Answers 2

2

What I recommend you look into / try is creating your own BaseAdapter. When you override this class it will give you all the functions you need to override to populate the list view.

This has the advantage of giving you complete control of what is put into the listview and how each view is created.

Then after you get it working I recommend looking into the ViewHolder design pattern along with recycling views which is a great way of improving efficiency when scrolling the listview.

Sign up to request clarification or add additional context in comments.

1 Comment

As it turns out, your advice is really reasonable, but I still can't understand how to use my model in my adapter and what constructor I actually need for this adapter.
1

What you are really looking for seem to be a MultiMap. To your first Question - your attemp was quite good, you can only put ArrayLists as values into your TreeMap.

The Problem with this might be, that if you want to add some ItemsModel to your Map, you first need to get the List of the key, and then add the ItemsModel to that list. Additionally you need to ensure, that this list for this particular key exist, and if not, create it.

Example:

String key = "hi";
ArrayList keyList = mapInstance.get(key);
if (keyList == null) {
   keyList = new ArrayList();
   mapInstance.put(key, keyList);
}
keyList.add(itemsModelInstance);

A get()/contains() and so on may be somehow equal. I'd suggest you build your own Multimap<?,?> Implementation or just take an existing one, like the one from Guava (link above).

Kind regards,

avi

1 Comment

Thank you, it was quite helpful.

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.