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.
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.
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..:)
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.
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);
Related
Currently, I have a full working app that displays the json data in the listview with default ListAdapter, however, I want to add a imageview and set the string data from the json data via web.
the data I get from the web is like {name=John,
title=my book, date_=2014/03/14}.
How can I set or make a custom adapter for to display json from arrList = new ArrayList<HashMap<String, String>>(); and also shows Images for the first 3 items ? Since, I'm quite new to this , I would need some help .
if(!arrList.isEmpty()){
ListAdapter adapter = new SimpleAdapter(JKujiListActivity.this, arrList,
R.layout.customlist, new String[] {"name", "title", "date"},
new int[] {R.id.name,R.id.dai, R.id.day});
mySpinner.setAdapter(adapter);
}
You can't do that with standart adapters. You should create your own adapter class as it explains here.
I am still unclear about which adapter to use in a situation that doesn't require the basic, SimpleAdapter. There are BaseAdapters, ArrayAdapters, CustomAdapters, etc.
I would like to make a ListView with a simple layout like seen in the comment section in the Google PlayStore. A TextView on one side, and an image that pops up a context menu of some sort.
What adapter would I use for this that would work best?
In your case it is easier to use a SimpleAdapter. Just provide your custom layout and connect the data with the widgets' id. Something similar to this:
List<HashMap<String,String>> datalist = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String,String>();
map.put("text", "some text");
map.put("image",Integer.toString( R.drawable.your_image_to_popup_a_menu ));
datalist.add( map );
String[] from = { "text","image" };
int[] to = { R.id.txt,R.id.img };
SimpleAdapter adapter = new SimpleAdapter( this, datalist, R.layout.your_layout, from, to);
Do not forget to identify the layout's TextView and ImageView as "#+id/txt" and "#+id/img" respectively.
(If your image is always the same, just set it in the layout and forget linking the R.id.img part)
I did listview tutorial. I can't see how to integrate the list making program into another program I have.
I want to fill an array with sensor input values in one method of my public class, and then display the array as a list after that.
Is it possible to call the list making code as a function, in response to some user activity within the 'sensor' method? How do I do this?
Excuse me if this is stupid, I am a beginner to Java.
Any advice appreciated.
AFAIK, the way you construct a listview is via List<? extends Map<String, ?>>.
if you do want to construct it this way, here's how to do it.
fill the data inside a List<Map<String,?>>, then use a SimpleAdapter (or other adapter) to concatenate the list to ListView. suppose you have a List<Map<String,?>> named mList,
ListView mListView = (ListView)findViewById( --listView id-- );
String[] mFrom = { -key1-, -key2- };
int[] mTo = {android.R.id.text1, android.R.id.text2 };
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, android.R.layout.simple_list_item_2, mFrom, mTo);
mListView.setAdapter(mAdapter);
listView id is the id of the listview on the xml, key1 and key2 respectively are the data you want to input inside the listview. however this one used android's default feature of list.
if you want to use your own listview template (i.e. you have more than 2 data inside),
define the xml of your custom listview (customlist.xml) then change the mTo variable to match your textviews
int[] mTo = {R.id.-listTextView1-,R.id.-listTextView2-,R.id.-listTextView3-};
and point to that xml on this line
SimpleAdapter mAdapter = new SimpleAdapter(getApplicationContext(), mList, R.layout.--customlistname--, mFrom, mTo);
I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.
I'm using the following code to populate listview
Oncreate method:
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.more_custom_row_view, new String[] {"title","desc"}, new int[] {R.id.text1,R.id.text2});
populateList();
setListAdapter(adapter);
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("title","Previous Searches");
temp.put("desc", "View your search history");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("title","Settings");
temp1.put("desc", "Update your account settings");
list.add(temp1);
}
when i go to another activity and come back to this acitivity it duplicate list items
each time any one guide me what mistake am i doing?
The problem is that you are using a static list for your items and not clearing or reinstantiating it in your onCreate method.
The list will stay in memory as long as your program is running because it is defined static. If you are returning to the activity the items are again added to the list. You could use clear() to remove all items from the list in your populate list call before filling it again.
Simple, check adapter.isEmpty() before populating
This is a snippet from my own codebase.
private void populateWithAppPackages() {
//DON'T continue if the adapter is not empty; prevents duplicates
if(!mAdapter.isEmpty()){
return;
}
/* ... populate the list ... */
}
DON'T clear() the list adapter!!
clear() and repopulating list every time the activity or the fragment are visited is a very expensive set of operations. You would be better of intelligently populating the list as above.
removing final from the list resolved my problem.