Skip to content

Instantly share code, notes, and snippets.

@ssinss
Created March 13, 2013 04:29
android ADT template EfficientListAdapter
public class ItemAdapter extends BaseAdapter
{
// TODO replace with a collection of real data
private static final List<DummyItem> DATA = DummyItemContent.ITEMS;
private LayoutInflater mInflater;
public ItemAdapter(Context context)
{
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
/**
* @see android.widget.ListAdapter#getCount()
*/
public int getCount()
{
return DATA.size();
}
/**
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position)
{
return DATA.get(position);
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position)
{
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent)
{
// A ViewHolder keeps references to children views to avoid unneccessary
// calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no
// need
// to reinflate it. We only inflate a new View when the convertView
// supplied
// by ListView is null.
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_item_item, parent,
false);
// Creates a ViewHolder and store references to the two children
// views
// we want to bind data to.
holder = new ViewHolder();
// TODO store references to your views
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.subtitle = (TextView) convertView
.findViewById(R.id.subtitle);
holder.thumbnail = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
}
else
{
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// TODO Bind your data efficiently with the holder.
holder.title.setText(DATA.get(position).content);
holder.subtitle.setText(DATA.get(position).content);
holder.thumbnail.setImageResource(DATA.get(position).thumbnail);
return convertView;
}
static class ViewHolder
{
// TODO define members for each view in the item layout
TextView title;
TextView subtitle;
ImageView thumbnail;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment