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;
11 public class WeatherSpecificDataAdapter extends ArrayAdapter<WeatherSpecificDataEntry> {
12 private final int resource;
14 public WeatherSpecificDataAdapter(final Context context, final int resource) {
17 this.resource = resource;
21 public View getView(final int position, final View convertView,
22 final ViewGroup parent) {
24 // We need to get the best view (re-used if possible) and then
25 // retrieve its corresponding ViewHolder, which optimizes lookup
27 final View view = this.getWorkingView(convertView);
28 final ViewHolder viewHolder = this.getViewHolder(view);
29 final WeatherSpecificDataEntry entry = this.getItem(position);
32 // Setting the text view
33 viewHolder.headerView.setText(entry.getHeader());
34 viewHolder.bodyView.setText(entry.getBody());
40 private View getWorkingView(final View convertView) {
41 // The workingView is basically just the convertView re-used if possible
42 // or inflated new if not possible
43 View workingView = null;
45 if(null == convertView) {
46 final Context context = this.getContext();
47 final LayoutInflater inflater = (LayoutInflater)context.getSystemService
48 (Context.LAYOUT_INFLATER_SERVICE);
50 workingView = inflater.inflate(this.resource, null);
52 workingView = convertView;
58 private ViewHolder getViewHolder(final View workingView) {
59 // The viewHolder allows us to avoid re-looking up view references
60 // Since views are recycled, these references will never change
61 final Object tag = workingView.getTag();
62 ViewHolder viewHolder = null;
65 if((null == tag) || !(tag instanceof ViewHolder)) {
66 viewHolder = new ViewHolder();
68 viewHolder.headerView = (TextView) workingView
69 .findViewById(R.id.weather_data_entry_header);
70 viewHolder.bodyView = (TextView) workingView
71 .findViewById(R.id.weather_data_entry_body);
73 workingView.setTag(viewHolder);
76 viewHolder = (ViewHolder) tag;
83 * ViewHolder allows us to avoid re-looking up view references
84 * Since views are recycled, these references will never change
86 private static class ViewHolder {
87 public TextView headerView;
88 public TextView bodyView;