我有一个定制的 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" />