From faff51dd779a4ece284632228c459a92c396fa5c Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sun, 24 Jun 2012 00:27:31 +0200 Subject: [PATCH] New AdsEntryLatestAdapter no time to do it better. I should create a new class for AdsEntryAdapter in stead of two different class for the latest ads activity and the list actvity. No time right now... --- .../mobiads/list/AdsEntryLatestAdapter.java | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Android/MobiAdsReloaded/src/de/android/mobiads/list/AdsEntryLatestAdapter.java diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/AdsEntryLatestAdapter.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/AdsEntryLatestAdapter.java new file mode 100644 index 0000000..fe8927d --- /dev/null +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/AdsEntryLatestAdapter.java @@ -0,0 +1,107 @@ +package de.android.mobiads.list; + +import java.util.List; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import de.android.mobiads.R; + +/** + * Adapts AdsEntry objects onto views for lists + */ +public final class AdsEntryLatestAdapter extends ArrayAdapter { + + private final int adsItemLayoutResource; + + public AdsEntryLatestAdapter(final Context context, final int adsItemLayoutResource) { + super(context, 0); + this.adsItemLayoutResource = adsItemLayoutResource; + } + + public void setData(List data) { + clear(); + if (data != null) { + addAll(data); + } + } + + /** + * Populate new items in the list. + */ + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + + // We need to get the best view (re-used if possible) and then + // retrieve its corresponding ViewHolder, which optimizes lookup efficiency + final View view = getWorkingView(convertView); + final ViewHolder viewHolder = getViewHolder(view); + final AdsEntry entry = getItem(position); + + // Setting the text view + viewHolder.titleView.setText(entry.getTitle()); + + + viewHolder.textView.setText(entry.getText()); + + // Setting image view + viewHolder.imageView.setImageBitmap(entry.getIcon()); + + + return view; + } + + private View getWorkingView(final View convertView) { + // The workingView is basically just the convertView re-used if possible + // or inflated new if not possible + View workingView = null; + + if(null == convertView) { + final Context context = getContext(); + final LayoutInflater inflater = (LayoutInflater)context.getSystemService + (Context.LAYOUT_INFLATER_SERVICE); + + workingView = inflater.inflate(adsItemLayoutResource, null); + } else { + workingView = convertView; + } + + return workingView; + } + + private ViewHolder getViewHolder(final View workingView) { + // The viewHolder allows us to avoid re-looking up view references + // Since views are recycled, these references will never change + final Object tag = workingView.getTag(); + ViewHolder viewHolder = null; + + + if(null == tag || !(tag instanceof ViewHolder)) { + viewHolder = new ViewHolder(); + + viewHolder.titleView = (TextView) workingView.findViewById(R.id.ads_entry_title); + viewHolder.textView = (TextView) workingView.findViewById(R.id.ads_entry_text); + viewHolder.imageView = (ImageView) workingView.findViewById(R.id.ads_entry_icon); + + workingView.setTag(viewHolder); + + } else { + viewHolder = (ViewHolder) tag; + } + + return viewHolder; + } + + /** + * ViewHolder allows us to avoid re-looking up view references + * Since views are recycled, these references will never change + */ + private static class ViewHolder { + public TextView titleView; + public TextView textView; + public ImageView imageView; + } +} -- 2.1.4