Reputation: 631
I have a customlistadapter as follow:
public class CustomListViewAdapter2 extends ArrayAdapter<RowItem> {
List<Integer> baseOnThis;
public CustomListViewAdapter2(Context context, int resourceId,
List<RowItem> items, ArrayList<Integer> ids) {
super(context, resourceId, items);
this.context = context;
baseOnThis= ids;
}
/* private view holder class */
private class ViewHolder {
TextView firstHemistich;
TextView SecondHemistich;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.hemistich_rows, null);
holder = new ViewHolder();
holder.firstHemistich = (TextView) convertView
.findViewById(R.id.title);
holder.SecondHemistich = (TextView) convertView
.findViewById(R.id.desc);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.firstHemistich.setText(rowItem.getTitle());
holder.SecondHemistich.setText(rowItem.getDesc());
return convertView;
}
now I need base on the database value that saved on Arraylist of "baseOnThis" change the color of item of listview but I can't. Is there any idea how should I do this? Please let me know if my question is wrong and don't minus me
Upvotes: 4
Views: 1850
Reputation: 418
Maybe better would be to read a little bit about android Adapters Reference
Basicaly getView method works like for or foreach and returns View objects for every item in your array or list that you can modify as you want, you just need to make any required changes inside getView() method
About changing color you can get main Layout container from your already inflated convertView and change its background color property as you want
Upvotes: 0
Reputation: 12725
change your getView
-method to this:
public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; RowItem rowItem = getItem(position); LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.hemistich_rows, null); holder = new ViewHolder(); holder.firstHemistich = (TextView) convertView .findViewById(R.id.title); holder.SecondHemistich = (TextView) convertView .findViewById(R.id.desc); convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); holder.firstHemistich.setText(rowItem.getTitle()); holder.SecondHemistich.setText(rowItem.getDesc()); // change color of item if (yourValueHere) { holder.SecondHemistich.setTextColor(yourColor); } return convertView; }
change yourValueHere and yourColor... This will colorize each item based on the value yourValueHere.
Upvotes: 0
Reputation: 573
Try this
if(position == 3){
holder.SecondHemistich.setTextColor(this.context.getResources().getColor(R.color.color1));
}
Upvotes: 2