How to update SimpleAdapter in Android

Can I update SimpleAdapter? I have a list of data and a footer that says “See the Following Results”. When this list item is clicked, I commit the event and get new data. Then I want to replace the data in the ListView with this new data, but I cannot figure out how to do it. Any ideas? I do not want to use an ArrayAdapter because, as far as I see, elements can contain only one line, where I need it to contain several lines and ints.

+8
android listview listadapter
Jul 22 '10 at 20:52
source share
5 answers

Update. According to del116, you really can give the SimpleAdapter mutable map, and then manually call the notifyDataSetChanged method notifyDataSetChanged you need to update the list. However, my bottom point is for the SimpleAdapter documentation, indicating that it is for static data; using it for mutable data is contrary to its design, so if you use this method, I will definitely check if it continues to work in new versions of Android as they become available.

(The original comment is as follows :)

If you look at the description of the SimpleAdapter , it says that it is "a simple adapter for mapping static data to views defined in an XML file." I added the emphasis - simply: SimpleAdapater not designed for use with modified data; It processes only static data. If you cannot use the ArrayAdapter because your data has more than one bit of text, you will either have to create your own custom ListAdapter or put your data in the database and use one of CursorAdapter s.

As a last resort, if you don’t need much performance, you can update the ListView supported by the SimpleAdapter by creating a whole new instance of SimpleAdapter at any time when your data changes and tells you about viewing the list. use it through setListAdapter .

+10
Aug 04 '10 at 17:54
source share
 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 every time to update the ListView. Keep in mind that you need to update the list variable with new data.

Hope this helps ... :)

+3
May 23 '13 at 8:24
source share

SimpleAdapter is for static data, so your performance may vary. The best solution is probably to switch to another type of adapter, such as an ArrayAdapter , or to create a new SimpleAdapter each time the dataset changes.

+2
13 Oct. '12 at 20:38
source share

I was unable to get notifyDataSetChanged () to work with updating my SimpleAdapter, so instead I first tried to remove all the views that were attached to the parent layout using removeAllViews () and then add ListView and it worked, which allowed me to update the custom interface:

 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); 
0
Aug 22 '13 at 14:52
source share
 mySimpleAdapter.notifyDataSetChanged(); 

Use this line after updating data in the HashMap data list.

-2
Jul 21 '15 at 1:03
source share



All Articles