How change color of specific item of listview?

半世苍凉 提交于 2019-12-12 09:41:33

问题


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


回答1:


Try this

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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/23755522/how-change-color-of-specific-item-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!