简体   繁体   中英

How change color of specific item of listview?

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

Try this

if(position == 3){
        holder.SecondHemistich.setTextColor(this.context.getResources().getColor(R.color.color1));
}   

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM