Skip to content

Instantly share code, notes, and snippets.

@pavlospt
Created July 8, 2014 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavlospt/cd548155a96fbb753b7c to your computer and use it in GitHub Desktop.
Save pavlospt/cd548155a96fbb753b7c to your computer and use it in GitHub Desktop.
CustomAdapter for my Blog
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//If convertView is null, we inflate our Layout in convertView.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_layout, parent, false);
//We instantiate our ViewHolder object, in order to use it below.
holder = new ViewHolder();
//Let's say that our custom layout file contains only one TextView.
//So we use the ViewHolder pattern to instantiate it.
holder.mTextView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Because we extend ArrayAdapter, we can use the position and retrieve
//the current object using the getItem(int i) method.
String item = getItem(position);
//We only execute the following, if our item is not null.
if (item != null){
holder.mTextView.setText(item);
}
return convertView;
}
private static class ViewHolder {
TextView mTextView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment