ListActivity(with Inflate)
In Previous post, I explained how to use ListActivity.(ListActivity
Next, to use LayoutInflator, we can simplify Adapter codes.
Pick up only ArrayAdapter parts.
Sample1(Use Inflator)
class IconicAdapter extends ArrayAdapter<String> { IconicAdapter() { super(InflatorDynamicList.this, R.layout.row, R.id.label, items); // R.id.label : row's TextView } public View getView(int position, View convertView, ViewGroup parent ) { LayoutInflater inflater = getLayoutInflater(); View row = inflater.inflate(R.layout.row, parent, false); TextView label = (TextView)row.findViewById(R.id.label); label.setText(items[position] + " Love"); ImageView icon = (ImageView)row.findViewById(R.id.icon); if ( items[position].length() > 5 ) { icon.setImageResource(R.drawable.spade); } else { icon.setImageResource(R.drawable.heart); } return (row); } }
Sample2(optimization)
class IconicAdapter extends ArrayAdapter<String> { IconicAdapter() { super(RecyclingDemo.this, R.layout.row, R.id.label, items); // R.id.label : row's TextView } public View getView(int position, View convertView, ViewGroup parent ) { // Tech View row = convertView; if ( row == null ) { LayoutInflater inflater = getLayoutInflater(); row = inflater.inflate(R.layout.row, parent, false); } ImageView icon = (ImageView)row.findViewById(R.id.icon); TextView label = (TextView)row.findViewById(R.id.label); label.setText(items[position] + " Love"); if ( items[position].length() > 5 ) { icon.setImageResource(R.drawable.spade); } else { icon.setImageResource(R.drawable.heart); } return (row); } }
Reuse row view.