New AdsEntryLatestAdapter no time to do it better.
authorgumartinm <gustavo@gumartinm.name>
Sat, 23 Jun 2012 22:27:31 +0000 (00:27 +0200)
committergumartinm <gustavo@gumartinm.name>
Sat, 23 Jun 2012 22:27:31 +0000 (00:27 +0200)
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...

Android/MobiAdsReloaded/src/de/android/mobiads/list/AdsEntryLatestAdapter.java [new file with mode: 0644]

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 (file)
index 0000000..fe8927d
--- /dev/null
@@ -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<AdsEntry> {
+
+       private final int adsItemLayoutResource;
+
+       public AdsEntryLatestAdapter(final Context context, final int adsItemLayoutResource) {
+               super(context, 0);
+               this.adsItemLayoutResource = adsItemLayoutResource;
+       }
+
+       public void setData(List<AdsEntry> 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;
+       }
+}