CursorAdapter自动更新图像

时间:2013-04-09 11:23:14

标签: android listview simplecursoradapter

我有一个定制的SimpleCursorAdapter,可以从数据库中获取信息。如果一个值为1,我将为ImageView的背景着色,如果它为0,则不对其进行着色。加载ListView时,一切都是正确的,但如果我滚动列表中的项目,则会使图像颜色错误。

============刚加载============上下滚动后====

我知道ListView回收,当一个项目不在屏幕上时,它的资源被释放,但我不明白为什么当他们回到屏幕时加载错误。当我不希望它变成彩色时,我只是将ImageView颜色设置为透明:这是解决方法使其正常工作的唯一方法吗?

这是我的相关代码:

RecipeCursorAdapter.java

public class RecipeCursorAdaptor extends SimpleCursorAdapter  {

private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final LayoutInflater mLayoutInflater;

public CustomCursorAdaptor(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.mLayout = layout;
    this.mContext = context;
    this.mCursor = c;
    this.mLayoutInflater = LayoutInflater.from(mContext);
}

private final class ViewHolder {
    TextView txt_title;
    TextView txt_time;
    TextView txt_difficulty;
    ImageView img_color;
}

public View getView(int position, View convertView, ViewGroup parent) {
    if (mCursor.moveToPosition(position)) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(mLayout, null);

            viewHolder = new ViewHolder();
            viewHolder.img_color = (ImageView) convertView.findViewById(R.id.ricettaColore);
            viewHolder.txt_title = (TextView) convertView.findViewById(R.id.ricettaTitolo);
            viewHolder.txt_time = (TextView) convertView.findViewById(R.id.ricettaTempo);
            viewHolder.txt_difficulty = (TextView) convertView.findViewById(R.id.ricettaDifficolta);

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

        String title = mCursor.getString(1);
        String time = mCursor.getString(2);
        String difficulty = mCursor.getString(3);
        int vegetarian = mCursor.getInt(4);

        viewHolder.txt_title.setText(title);
        viewHolder.txt_time.setText(time);
        viewHolder.txt_difficulty.setText(difficulty);

        if(vegetarian == 1)
            viewHolder.img_color.setBackgroundColor(0xff669900);
             /* Adding these two lines it works properly 
             else
            viewHolder.img_color.setBackgroundColor(0x00000000);
             */

    }
    return convertView;
}

list_ricette.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/ricettaColore"
    android:layout_width="5dip"
    android:layout_height="45dip"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:contentDescription="Ricetta vegetariana" />

<TextView
    android:id="@+id/ricettaTitolo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    android:layout_toRightOf="@+id/ricettaColore"
    android:scrollHorizontally="false"
    android:singleLine="true"
    android:text="Titolo Ricetta"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/ricettaDifficolta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/ricettaTitolo"
    android:layout_below="@+id/ricettaTitolo"
    android:scrollHorizontally="false"
    android:singleLine="true"
    android:text="Difficoltà"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#A1A1A1" />

<TextView
    android:id="@+id/ricettaTempo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/ricettaDifficolta"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/ricettaTitolo"
    android:gravity="right"
    android:text="Tempo"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#A1A1A1" />

1 个答案:

答案 0 :(得分:0)

您需要使ViewHolder类保持静态,并在该视图持有者类中创建一个静态int数组。在getView中,您应该使用数据库中的值设置该数组,并使用该数组来设置您想要的行。

这就是我设法在ExpandableListView中保持复选框正确的方法:

static class ViewHolder {
    CheckBox check;
    static boolean[][] bool = new boolean[parentCounter][childCounter];

    public static void setChecked(int parent, int child, boolean value) {
        bool[parent][child] = value;
    }

    public static boolean getChecked(int parent, int child) {
        return bool[parent][child];
    }
}

这是相同的概念。