为每个列表视图行指定颜色

时间:2013-05-27 15:19:57

标签: java android listview background-color

我使用以下代码填充ListView:

    final ListView lv = (ListView) findViewById(R.id.lvKinder);
    ArrayList<Kind> kisi = datenfuerlistview();
    KinderlistAdapter kiadapter = new KinderlistAdapter(this, kisi);
    lv.setAdapter(kiadapter);
    final DBHelper db = new DBHelper(this);

现在我想分析我添加到Kind的每个ListView对象的一个​​条目。如果它有一定值,我想更改此ListView条目的backgroundcolor。

这是我的适配器:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null){
            convertView = inflator.inflate(R.layout.list_black_text, parent, false);
            holder = new ViewHolder((TextView) convertView.findViewById(R.id.nullline), (TextView) convertView.findViewById(R.id.firstLine), (ImageView) convertView.findViewById(R.id.imgv));

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        Kind ki = (Kind) getItem(position);
        holder.getName().setText(ki.getName());
        holder.getDatum().setText(ki.getDatum());

        Uri uri = Uri.parse(ki.getGeschlecht());

        String path = ki.getKinderbild();

        System.out.println("HSSSSSSSSSSSSSSSSSSSSSS" + path);
        //holder.getGeschlecht().setImageBitmap(bitmap);

        ContentResolver cr = context.getContentResolver();
        Bitmap tmp = null;
        try {
            tmp = getThumbnail(cr, path);
        } catch (Exception e) {
            System.out.println("NAAAAAAAAAAAAAAAAAAAA") ;
            e.printStackTrace();
        }

        holder.getGeschlecht().setImageBitmap(tmp);

        return convertView;

如何设置颜色?

1 个答案:

答案 0 :(得分:0)

你试过这个吗? (在返回convertView之前):

convertView.setBackgroundColor(Color.BLUE);//or another color