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)
Related
I have two TextViews in a ListView ,bind data by SimpleAdapter,I want to show different color in a single TextView,I use Html.fromHtml,but style can't work,so pls help..
simpleAdapter = new SimpleAdapter(NewsListActivity.this, list, R.layout.list_item, new String[] { "title", "source" }, new int[] { R.id.tvTitle, R.id.tvSource });
listview.setAdapter(simpleAdapter);
ArrayList<Map<String, Object>> list= new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", Html.fromHtml("<u>it's an another day.</u>"));
map.put("source", Html.fromHtml("<font color=#ffcc00>bbc news</font>"));
list.add(map);
You'll have to implement a custom ListViewAdapter. In getView(), you can then instantiate your own row layout and set the text color.
Information on custom Adapters can be found e.g. here: http://www.vogella.com/tutorials/AndroidListView/article.html
As #FD suggested you need a custom ListView containing a TextView, then in that Text View you can have a sentence with different colors using the code below.
textView.setText(Html.fromHtml("<font color='red'>First line</font><br/><font color='blue'>Second line</font>"));
I have implemented a Section header Listview where i inflate a layout which have three textviews in it as item.i have referred this example here to achieve it.
My code is
adapter.addSection(monthitems2[l], new SimpleAdapter(this, security, R.layout.phone_row,
new String[] {"Title", "Caption","val1" }, new int[] { R.id.tvContact, R.id.tvMobile,R.id.tvMail }));
I have a condition where i need to change text color of ID "R.id.tvContact".I havent defined the textview anywhere in thelayout.i have just referred it as simpleadapter parameter.How to change the textview color the default is white.Any suggestions answers are highly appreciated.
From the reference tutorial, you have
SeparatedListAdapter adapter = new SeparatedListAdapter(this);
adapter.addSection("Array test", new ArrayAdapter<String>(this,
R.layout.list_item, new String[] { "First item", "Item two" }));
adapter.addSection("Security", new SimpleAdapter(this, security, R.layout.list_complex,
new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));
ListView list = new ListView(this);
list.setAdapter(adapter);
this.setContentView(list);
Get a handle to your TextView, then set its color from that handle. You need to Post the
commands to the message queue, in a runnable, b/c at this point in the code the ListView hasn't yet rendered the TextView's that the adapter points to (that will happen sometime after OnCreate exits).
list.Post(() =>
{
TextView tv = list.FindViewById<TextView>(R.id.tvContact);
tv.SetTextColor(Color.Red);
});
I don't think you need to qualify FindViewById with list. b/c you set the content view with
this.setContentView(list);
(above code in C# for MonoDroid, easily convertable to Java).
Posting runnables in Java/Android: How to run a Runnable thread in Android?
Posting in runnables puts the code in the message queue, a key, undocumented construct in Android. Anytime you get a null reference from an activity when modifying Views, try this as a first step.
I need to put small icons on the right of each list item. I know it can be done with a custom list but it will mean rewiring a lot of code. if there a way to do it without using a custom adapter?
mList = (ListView) findViewById(R.id.sidebar_list);
mList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mStrings));
mList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
No, overriding getView inside a custom adapter is the only way to dynamically set different images to your list rows. However, if you want to set a static (same) image for all the row items, then you may define your custom resource xml for the row (with a TextView and ImageView or comppund drawable-TextView inside) and pass it to your default adapter when initializing.
You will need to create XML layout for the view (if you don't have one) and add an imageview.
you should give it and ID ('thubnail').Next you just ask the adapter for a certain view by its position
View singleView = listView.getChildAt(wantedChild);
ImageView thumbnail = (ImageView) singleView.findViewById(R.id.thumbnail);
thumbnail.setImageBitmap(bitmap);
Yes, you can use SimpleAdapter and a custom layout with an ImageView.
Trivial example:
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "one");
map.put("image", String.valueOf(getResources().getIdentifier("ic_launcher", "drawable", getPackageName())));
list.add(map);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new SimpleAdapter(this, list, R.layout.list_item, new String[] {"text", "image"}, new int[] {R.id.text, R.id.image}));
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);
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);