是否可以仅为列表视图中的特定项目着色?

时间:2018-02-07 11:24:50

标签: android listview

我有一个问题。我有一组对象,我已经添加到ArrayAdapter和listview中的适配器。我的listView在奇数位置上着色,甚至在另一种颜色上着色。 现在是棘手的部分。 我的对象由字符串id和名称,字符串注释和日期组成。 我可以为具有id / name预设的条目着色吗? 例如,如果我登录并且我想查看列表视图,那么我出现的条目(我已经保留了具有共享首选项的id)将用另一种颜色着色。从5个条目开始,让我们说,我出现在其中3个,随机位置。如何为我出现的位置着色?

谢谢!

1 个答案:

答案 0 :(得分:1)

我做了一些非常相似的事情。我想更改字体颜色而不是整个项目,但您可以用您的代码替换我的代码。

@Override
public View getView(int position, View v, ViewGroup parent)
{
    View mView = v ;
    if(mView == null){
        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(id, null);
    }

    TextView text = (TextView) mView.findViewById(R.id.textView);

    if(items.get(position) != null ) //here you can match IDs and change the accordingly 
    {
        text.setTextColor(Color.WHITE);
        text.setText(items.get(position));
        text.setBackgroundColor(Color.RED); 
        int color = Color.argb( 200, 255, 64, 64 );
            text.setBackgroundColor( color );

    }

    return mView;
}