Android的ListView的行颜色颜色、Android、ListView

2023-09-06 13:08:21 作者:兴你一世烟花

我想改变我的customAdapter的ListView的行颜色。有整数,包括0和1,我想从阵列读取和改变行的颜色像这样的数组:

I'm trying to change the row color of my listView in customAdapter. There's an array of integer that include 0 and 1, I'm trying to read from the array and change the color of rows like this:

0 =白

1 =黄

但它显示了一个黄色的所有行。 这是我的CustomAdapter:

but it shows a yellow color in all rows. This is my CustomAdapter:

package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<RowItem> {

ArrayList<Integer> test;

Context context;

public CustomAdapter(Context context, int resourceId, List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

/* private view holder class */
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);


    SqliteHelper checkFav = new SqliteHelper(context);
    checkFav.open();
    test = checkFav.getFavForList();
    checkFav.close();

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_layout, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.pTxt);

        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);


    } else


        holder = (ViewHolder) convertView.getTag();

                    int color0 = Color.YELLOW;
        int colorDefault = Color.WHITE;

        switch (test.get(position)){
        case 0:
            convertView.setBackgroundColor(colorDefault);
            break;
        case 1:
            convertView.setBackgroundColor(color0);
        }       
            holder.txtTitle.setText(rowItem.getTitle());

    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

}

这是我row_layout.xml:

And this is my row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/active_icon" />

    <TextView
        android:id="@+id/pTxt"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="20sp"
        android:gravity="right"/>
</LinearLayout>

在此先感谢。

Thanks in advance.

推荐答案

快速的解决方案(不厚道code,但工程):

Fast solution (not very nice code, but works):

@Override
public int getItem(int position){
  return test.get(position);
}


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

  int color0 = ....
  int color1 = ....
  int colorDefault = ...

  switch (test.get(position)) {
     case 0:
             convretview.setBackgroundColor(color0);
             break;
     case 1:
            convretview.setBackgroundColor(color1);
            break;
     default:
           convretview.setBackgroundColor(colorDefault); 
  }
  ...
}