I contribute to an open source project, which uses a ListView
to show some data, with each item containing 2 TextView
items R.id.card_column1
and R.id.card_column2
, so that we see a 2 column list representing some data for a large number of flashcards.
We have a fixed data structure ArrayList<HashMap<String, String>> mCards
containing various key/value pairs which could be shown in the columns, and use a SimpleAdapter
to map between a given key/value pair in the HashMap of the mCards
elements, and the TextView
items as follows:
mCardsAdapter = new SimpleAdapter(
this,
mCards,
R.layout.card_item_browser,
new String[] {col1Key, col2Key},
new int[] {R.id.card_column1, R.id.card_column2});
The data mapping between the fixed mCards
structure and the 2 TextView
elements making up the columns needs to be adjustable, i.e. we need to be able to change col1Key
and col2Key
. I couldn't find any methods for changing the from
keys in the SimpleAdapter
documentation, so I currently have implemented this by simply calling the setAdapter()
method of the ListView
with a new SimpleAdapter
containing the updated keys, whenever the user asks to change the column. The problem with this approach is that the vertical position in the ListView
is reset.
One solution could be to change the data structure itself, and call notifyDataSetChanged()
, however the array can be extremely large, so this approach would be a waste of valuable RAM, and may require significant CPU time to update the data structure, so I want to avoid this approach.
So my questions are:
1) Is it possible to dynamically change the mapping in the adapter, i.e. change the from
object in the constructor, without creating a new SimpleAdapter
?
2) If not, what's the best way to change the mapping without losing the vertical scrolling position in the ListView
? Preferably without consuming any extra RAM, or requiring more CPU than creating a new adapter.