CursorAdapter的自动更新图片 [英] CursorAdapter autoupdates images

查看:127
本文介绍了CursorAdapter的自动更新图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定制的SimpleCursorAdapter,从数据库中获取信息。如果有一个值为1 i色的ImageView的背景下,如果是0,我不色的。当ListView控件加载一切都是正确的,但这时如果我在滚动列表中的项目都获得图像的彩色错了。

I have a customized SimpleCursorAdapter that gets information from a database. If one value is 1 I color the background of an ImageView, if it's 0 I don't color it. When the ListView is loaded everything is correct but then if I scroll the items in the list are get the image colored wrong.

============刚刚加载============了一个滚动和向下后====

============Just loaded============ After one scroll up and down====

我知道的ListView回收和,当一个产品从它的资源被释放的画面,但我不知道为什么,当他们回到屏幕加载错误的。我得到它的工作简单地设置ImageView的颜色为透明时,我不希望它有色:?这是解决办法,使工作权的唯一途径。

I know about ListView recycling and that when one item is out of the screen it's resources are released, but I don't get why when they get back to the screen are loaded wrong. I got it working simply setting the ImageView color to transparent when I don't want it colored: is this workaround the only way to make it work right?

这是我的相关code:

This is my relevant code:

RecipeCursorAdapter.java

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

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" />

推荐答案

你想使你的ViewHolder类的静态和创建视图持有者类中的静态int数组。在getView你应该设置阵列从数据库中你的价值,也可以使用该数组像你想让它设置行。

You'll want to make your ViewHolder class static and create a static int array inside that view holder class. In the getView you should set that array with your value from the database and also use that array to set the row like you want it.

这是我如何设法保持在ExpandableListView正确的复选框:

This is how I managed to keep checkboxes correct in an 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];
    }
}

这是同一个概念。

It's the same concept.

这篇关于CursorAdapter的自动更新图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆