Created
March 1, 2014 14:06
-
-
Save vjdhama/9290205 to your computer and use it in GitHub Desktop.
ViewHolder Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.mally.volleytest; | |
import java.util.ArrayList; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
public class OfferAdapter extends ArrayAdapter<Offer> { | |
// View lookup cache | |
private static class ViewHolder { | |
TextView offerMain; | |
TextView offerDesc; | |
} | |
public OfferAdapter(Context context) { | |
super(context, R.layout.offer_view); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
// Get the data item for this position | |
Offer offer = getItem(position); | |
// Check if an existing view is being reused, otherwise inflate the view | |
ViewHolder viewHolder; // view lookup cache stored in tag | |
if (convertView == null) { | |
viewHolder = new ViewHolder(); | |
LayoutInflater inflater = LayoutInflater.from(getContext()); | |
convertView = inflater.inflate(R.layout.offer_view, null); | |
viewHolder.offerMain = (TextView) convertView.findViewById(R.id.offerMainTextView); | |
viewHolder.offerDesc = (TextView) convertView.findViewById(R.id.offerDescTextView); | |
convertView.setTag(viewHolder); | |
} else { | |
viewHolder = (ViewHolder) convertView.getTag(); | |
} | |
// Populate the data into the template view using the data object | |
viewHolder.offerMain.setText(offer.getOfferHead()); | |
viewHolder.offerDesc.setText(offer.getOfferDesc()); | |
// Return the completed view to render on screen | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment