How to update SimpleAdapter in Android
Asked Answered
G

4

9

Is it possible to update a SimpleAdapter? I have a list of data and a footer that says "See Next Results" When that list item is clicked I capture the event and get new data. I then want to replace the data in the ListView with this new data but I can't figure out how to do it. Any Ideas? I don't want to use an ArrayAdapter, cause as far as I can see the items can only hold one string where I need it to hold multiple strings and ints.

Gogol answered 22/7, 2010 at 20:52 Comment(0)
G
11

Update: According to del116, you can indeed give SimpleAdapter a mutable map and then manually call the adapter's notifyDataSetChanged method when you need the list to update. However, my point below stands about the documentation of SimpleAdapter specifying that it is for static data; using it for mutable data is going counter to its design, so if you use this technique I would be sure to check on whether it continues to work in new Android releases as they emerge.

(Original commentary follows:)

If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.

As a last resort, if you don't need much performance, you could update a ListView backed by a SimpleAdapter by building a whole new SimpleAdapter instance any time your data changes and telling the list view to use it via setListAdapter.

Gatha answered 4/8, 2010 at 17:54 Comment(3)
Your answer here is really misleading. After updating the array data and calling notifyDataSetChanged() on my SimpleAdapter, I was able to update the simpleAdapter. No need to use ArrayAdapter to call notifyDataSetChanged().Protohistory
@Protohistory Fair enough. I referred to that possibility in the end of my answer, but I've promoted it to the top based on your feedback that it does in fact work; I had not tested it. I still maintain that doing such a thing merits some caution since the SimpleAdapter documentation specifically states that it is for static data.Gatha
Looking back at this now...wow. I knew nothing about programming or the intended purpose of SimpleAdapter. If you're using a ListView (or any AdapterView) backed by a data structure that will change don't use SimpleAdapter. Foot.inMouth(true);Protohistory
A
3
ListView lv= (ListView) findViewById(R.id.loglist);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "time", "message" };
int[] to = { R.id.logtime, R.id.logmessage };

adapter = new SimpleAdapter(getApplicationContext(), list,R.layout.log_list_row, from,to);
lv.setAdapter(adapter);

Call this function each time to update the ListView. Keep in Mind that You have to update the list variable with new Data..

Hope this Helps..:)

Ala answered 23/5, 2013 at 8:24 Comment(0)
B
2

SimpleAdapter is meant for static data, so your performance may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.

Biofeedback answered 13/10, 2012 at 20:38 Comment(0)
E
0

I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:

LinearLayout results = (LinearLayout)findViewById(R.id.results);
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row, 
                    new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );

for (...) { 
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("name", name);
    map.put("dept", dept);
    list.add(map);
}

lv.setAdapter(adapter);
results.removeAllViews();     
results.addView(lv);
Englishry answered 22/8, 2013 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.