After creating a list view and assigning the text of each item with a different colour, inflater.inflate(R.layout.list_item, parent, false)
within View rowView = inflater.inflate(R.layout.list_item, parent, false);
becomes highlighted in yellow and I get a warning associated with my view holder. What should the affected code be changed to in order to get rid of this warning?
Unconditional layout inflation from view adapter: Should use View Holder pattern for smoother scrolling
public class FragmentColourChooserList extends android.support.v4.app.Fragment {
public FragmentColourChooserList() {
// Required empty constructor
}
ListView list_colourchooser;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_colour_chooser_list, container, false);
String[] listContent = {
getActivity().getResources().getString(R.string.item_0),
getActivity().getResources().getString(R.string.item_1),
getActivity().getResources().getString(R.string.item_2),
getActivity().getResources().getString(R.string.item_3),
getActivity().getResources().getString(R.string.item_4),
getActivity().getResources().getString(R.string.item_5),
getActivity().getResources().getString(R.string.item_6),
getActivity().getResources().getString(R.string.item_7),
getActivity().getResources().getString(R.string.item_8),
getActivity().getResources().getString(R.string.item_9)
};
list_colourchooser = (ListView)v.findViewById(R.id.list_colourchooser);
MyColoringAdapter adapter = new MyColoringAdapter(getActivity(),listContent);
list_colourchooser.setAdapter(adapter);
return v;
}
private class MyColoringAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyColoringAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
class Holder
{
TextView txtView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
Holder holder = new Holder();
holder.txtView = (TextView) rowView.findViewById(R.id.list_item);
txtView.setText(values[position]);
int textColorId = R.color.white; // Default color
switch (position) {
case 0:
textColorId = R.color.brown; break;
case 1:
textColorId = R.color.red; break;
case 2:
textColorId = R.color.yellow; break;
case 3:
textColorId = R.color.green; break;
case 4:
textColorId = R.color.pink; break;
case 5:
textColorId = R.color.grey; break;
case 6:
textColorId = R.color.purple; break;
case 7:
textColorId = R.color.white; break;
case 8:
textColorId = R.color.darkblue; break;
case 9:
textColorId = R.color.lightblue; break;
}
txtView.setTextColor(getResources().getColor(textColorId));
return rowView;
}
}
}