1 package de.example.exampletdd.fragment.overview;
3 import android.content.Context;
4 import android.view.LayoutInflater;
5 import android.view.View;
6 import android.view.ViewGroup;
7 import android.widget.ArrayAdapter;
8 import android.widget.ImageView;
9 import android.widget.TextView;
10 import de.example.exampletdd.R;
12 public class OverviewAdapter extends ArrayAdapter<OverviewEntry> {
13 private final int resource;
15 public OverviewAdapter(final Context context, final int resource) {
18 this.resource = resource;
22 public View getView(final int position, final View convertView,
23 final ViewGroup parent) {
25 // We need to get the best view (re-used if possible) and then
26 // retrieve its corresponding ViewHolder, which optimizes lookup
28 final View view = this.getWorkingView(convertView);
29 final ViewHolder viewHolder = this.getViewHolder(view);
30 final OverviewEntry entry = this.getItem(position);
34 viewHolder.dateNameView.setText(entry.getDateName());
35 viewHolder.dateNumberView.setText(entry.getDateNumber());
37 // Setting temperature max/min
38 viewHolder.temperatureMaxView.setText(entry.getMaxTemp());
39 viewHolder.temperatureMinView.setText(entry.getMinTemp());
42 viewHolder.pictureView.setImageBitmap(entry.getPicture());
48 private View getWorkingView(final View convertView) {
49 // The workingView is basically just the convertView re-used if possible
50 // or inflated new if not possible
51 View workingView = null;
53 if(null == convertView) {
54 final Context context = this.getContext();
55 final LayoutInflater inflater = (LayoutInflater)context.getSystemService
56 (Context.LAYOUT_INFLATER_SERVICE);
58 workingView = inflater.inflate(this.resource, null);
60 workingView = convertView;
66 private ViewHolder getViewHolder(final View workingView) {
67 // The viewHolder allows us to avoid re-looking up view references
68 // Since views are recycled, these references will never change
69 final Object tag = workingView.getTag();
70 ViewHolder viewHolder = null;
73 if((null == tag) || !(tag instanceof ViewHolder)) {
74 viewHolder = new ViewHolder();
76 viewHolder.dateNameView = (TextView) workingView
77 .findViewById(R.id.weather_main_entry_date_name);
78 viewHolder.dateNumberView = (TextView) workingView
79 .findViewById(R.id.weather_main_entry_date_number);
80 viewHolder.temperatureMaxView = (TextView) workingView
81 .findViewById(R.id.weather_main_entry_temperature_max);
82 viewHolder.temperatureMinView = (TextView) workingView
83 .findViewById(R.id.weather_main_entry_temperature_min);
84 viewHolder.pictureView = (ImageView) workingView
85 .findViewById(R.id.weather_main_entry_image);
87 workingView.setTag(viewHolder);
90 viewHolder = (ViewHolder) tag;
97 * ViewHolder allows us to avoid re-looking up view references
98 * Since views are recycled, these references will never change
100 private static class ViewHolder {
101 public TextView dateNameView;
102 public TextView dateNumberView;
103 public TextView temperatureMaxView;
104 public TextView temperatureMinView;
105 public ImageView pictureView;