()).getItem(position));
}
public void onListItemClick(ListView parent, View v,
int position, long id) {
selection.setText(getModel(position));
}
class IconicAdapter extends ArrayAdapter<String> {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, items);
this.context = context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
View row = convertView;
ViewWrapper wrapper = null;
if (row==null) {
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.row, null);
wrapper = new ViewWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ViewWrapper)row.getTag();
}
wrapper.getLabel().setText (getModel(position));
if (getModel(position).length() > 4) {
wrapper.getIcon().setImageResource (R.drawable.delete);
} else {
wrapper.getIcon().setImageResource (R.drawable.ok);
}
return(row);
}
}
}
Just as we check convertView
to see if it is null in order to create the row Views
as needed, we also pull out (or create) the corresponding row’s ViewWrapper
. Then accessing the child widgets is merely a matter of calling their associated methods on the wrapper.
Making a List…
Lists with pretty icons next to them are all fine and well. But can we create ListView
widgets whose rows contain interactive child widgets instead of just passive widgets like TextView
and ImageView
? For example, could we combine the RatingBar
with text in order to allow people to scroll a list of, say, songs and rate them right inside the list?
There is good news and bad news.
The good news is that interactive widgets in rows work just fine. The bad news is that it is a little tricky, specifically when it comes to taking action when the interactive widget’s state changes (e.g., a value is typed into a field). We need to store that state somewhere, since our RatingBar
widget will be recycled when the ListView
is scrolled. We need to be able to set the RatingBar
state based upon the actual word we are viewing as the RatingBar
is recycled, and we need to save the state when it changes so it can be restored when this particular row is scrolled back into view.
What makes this interesting is that, by default, the RatingBar
has absolutely no idea what model in the ArrayAdapter
it is looking at. After all, the RatingBar
is just a widget, used in a row of a ListView
. We need to teach the rows which model they are currently displaying, so when their checkbox is checked they know which model’s state to modify.
So, let’s see how this is done, using the activity in the FancyLists/RateList
sample project at http://apress.com/. We’ll use the same basic classes as our previous demo—we’re showing a list of nonsense words, which you can then rate. In addition, words given a top rating will appear in all caps.
public class RateListDemo extends ListActivity {
TextView selection;
String[] items={'lorem', 'ipsum', 'dolor', 'sit', 'amet',
'consectetuer', 'adipiscing', 'elit', 'morbi', 'vel',
'ligula', 'vitae', 'arcu', 'aliquet', 'mollis',
'etiam', 'vel', 'erat', 'placerat', 'ante',
'porttitor', 'sodales', 'pellentesque', 'augue',
'purus'};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ArrayList<RowModel> list = new ArrayList<RowModel> ();
for (String s : items) {
list.add(new RowModel(s));
}