1 package name.gumartinm.weather.information.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;
11 import name.gumartinm.weather.information.R;
13 public class OverviewAdapter extends ArrayAdapter<OverviewEntry> {
14 private final int resource;
16 public OverviewAdapter(final Context context, final int resource) {
19 this.resource = resource;
23 public View getView(final int position, final View convertView,
24 final ViewGroup parent) {
26 // We need to get the best view (re-used if possible) and then
27 // retrieve its corresponding ViewHolder, which optimizes lookup
29 final View view = this.getWorkingView(convertView);
30 final ViewHolder viewHolder = this.getViewHolder(view);
31 final OverviewEntry entry = this.getItem(position);
35 viewHolder.dateNameView.setText(entry.getDateName());
36 viewHolder.dateNumberView.setText(entry.getDateNumber());
38 // Setting temperature max/min
39 viewHolder.temperatureMaxView.setText(entry.getMaxTemp());
40 viewHolder.temperatureMinView.setText(entry.getMinTemp());
43 viewHolder.pictureView.setImageBitmap(entry.getPicture());
49 private View getWorkingView(final View convertView) {
50 // The workingView is basically just the convertView re-used if possible
51 // or inflated new if not possible
52 View workingView = null;
54 if(null == convertView) {
55 final Context context = this.getContext();
56 final LayoutInflater inflater = (LayoutInflater)context.getSystemService
57 (Context.LAYOUT_INFLATER_SERVICE);
59 workingView = inflater.inflate(this.resource, null);
61 workingView = convertView;
67 private ViewHolder getViewHolder(final View workingView) {
68 // The viewHolder allows us to avoid re-looking up view references
69 // Since views are recycled, these references will never change
70 final Object tag = workingView.getTag();
71 ViewHolder viewHolder = null;
74 if((null == tag) || !(tag instanceof ViewHolder)) {
75 viewHolder = new ViewHolder();
77 viewHolder.dateNameView = (TextView) workingView
78 .findViewById(R.id.weather_main_entry_date_name);
79 viewHolder.dateNumberView = (TextView) workingView
80 .findViewById(R.id.weather_main_entry_date_number);
81 viewHolder.temperatureMaxView = (TextView) workingView
82 .findViewById(R.id.weather_main_entry_temperature_max);
83 viewHolder.temperatureMinView = (TextView) workingView
84 .findViewById(R.id.weather_main_entry_temperature_min);
85 viewHolder.pictureView = (ImageView) workingView
86 .findViewById(R.id.weather_main_entry_image);
88 workingView.setTag(viewHolder);
91 viewHolder = (ViewHolder) tag;
98 * ViewHolder allows us to avoid re-looking up view references
99 * Since views are recycled, these references will never change
101 private static class ViewHolder {
102 public TextView dateNameView;
103 public TextView dateNumberView;
104 public TextView temperatureMaxView;
105 public TextView temperatureMinView;
106 public ImageView pictureView;