1 package de.example.exampletdd.fragment.specific;
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.TextView;
9 import de.example.exampletdd.R;
10 import de.example.exampletdd.fragment.specific.WeatherSpecificDataEntry;
12 public class WeatherSpecificDataAdapter extends ArrayAdapter<WeatherSpecificDataEntry> {
13 private final int resource;
15 public WeatherSpecificDataAdapter(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 WeatherSpecificDataEntry entry = this.getItem(position);
33 // Setting the text view
34 viewHolder.headerView.setText(entry.getHeader());
35 viewHolder.bodyView.setText(entry.getBody());
41 private View getWorkingView(final View convertView) {
42 // The workingView is basically just the convertView re-used if possible
43 // or inflated new if not possible
44 View workingView = null;
46 if(null == convertView) {
47 final Context context = this.getContext();
48 final LayoutInflater inflater = (LayoutInflater)context.getSystemService
49 (Context.LAYOUT_INFLATER_SERVICE);
51 workingView = inflater.inflate(this.resource, null);
53 workingView = convertView;
59 private ViewHolder getViewHolder(final View workingView) {
60 // The viewHolder allows us to avoid re-looking up view references
61 // Since views are recycled, these references will never change
62 final Object tag = workingView.getTag();
63 ViewHolder viewHolder = null;
66 if((null == tag) || !(tag instanceof ViewHolder)) {
67 viewHolder = new ViewHolder();
69 viewHolder.headerView = (TextView) workingView
70 .findViewById(R.id.weather_data_entry_header);
71 viewHolder.bodyView = (TextView) workingView
72 .findViewById(R.id.weather_data_entry_body);
74 workingView.setTag(viewHolder);
77 viewHolder = (ViewHolder) tag;
84 * ViewHolder allows us to avoid re-looking up view references
85 * Since views are recycled, these references will never change
87 private static class ViewHolder {
88 public TextView headerView;
89 public TextView bodyView;