0

我的 CursorAdapter 数据更新有问题。
当我按下列表视图行项目的“星”img 时,我需要在数据库列“isFav”(0 到 1,反之亦然)中进行更改并显示已完成的星或空闲星(例如最喜欢的复选框)。但是当我按下星号时 - 当前行星号没有更新,但显示的最后一行会受到影响,就像我按下它一样。截图:http ://tinypic.com/r/2eph8uf/5

以下代码放置在下面:

private class MyAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;


public MyAdapter(Context context, Cursor c, boolean autoRequery) {
    super(context, c, autoRequery);
    mContext = context;
    mLayoutInflater = LayoutInflater.from(context); 
}

News tmp = new News(0,0,null,null,0,0);

@Override
public void bindView(View v, Context context, Cursor c) {
    tmp.setName(c.getString(c.getColumnIndexOrThrow(dbHelper.NAME)));
    tmp.setDescr(c.getString(c.getColumnIndexOrThrow(dbHelper.DESCRIPTION)));
    tmp.setPubdate(c.getLong(c.getColumnIndexOrThrow(dbHelper.DATE)));
    tmp.setGuid(c.getInt(c.getColumnIndexOrThrow(dbHelper.UID)));
    tmp.setIsFav(c.getInt(c.getColumnIndexOrThrow(dbHelper.IS_FAV)));
    tmp.setIsRead(c.getInt(c.getColumnIndexOrThrow(dbHelper.IS_READ)));

    TextView title_text = (TextView) v.findViewById(R.id.text);
    if (title_text != null) {
        title_text.setText(tmp.getName() + " " + convertLongToDate(tmp.getPubdate()));
    }

    title_text.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent _intent = new Intent(getActivity(), ItemActivity.class);
            _intent.putExtra("title", tmp.getName() + " " + Long.toString(tmp.getPubdate()));
            _intent.putExtra("desc", tmp.getDescr());
            startActivity(_intent);
            if (tmp.getIsRead() == 1)
                return;

            tmp.setIsRead(1);
            //working wrong!
            ContentValues cv = getContValues(tmp);
            loader.update(dbHelper.DATABASE_NAME, cv, dbHelper.UID + "=" + cv.getAsInteger("_id"), null);   
        //  db.UpdateItem(db.getWritableDatabase(), tmp);
        //  getLoaderManager().initLoader(0, null, mCallbacks);
        }
    });


    final ImageView fav_img = (ImageView) v.findViewById(R.id.image);
    if (fav_img != null)
    {
        if (tmp.getIsFav() != 0) {
            fav_img.setImageResource(R.drawable.fav);
        }
        else fav_img.setImageResource(R.drawable.unfav);
    }

    fav_img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (tmp.getIsFav() == 1)
            {
                tmp.setIsFav(0);
                fav_img.setImageResource(R.drawable.unfav);
            }
            else 
            {
                tmp.setIsFav(1);
                fav_img.setImageResource(R.drawable.fav);
            }
            Log.d("fav:", Integer.toString(tmp.getIsFav()) + " id: " + Integer.toString(tmp.getGuid()));
        //  db.UpdateItem(db.getWritableDatabase(), tmp);
        //  getLoaderManager().restartLoader(0, null, mCallbacks);
            //Working wrong!
            ContentValues cv = getContValues(tmp);
            loader.update(dbHelper.DATABASE_NAME, cv, dbHelper.UID + "=" + cv.getAsInteger("_id"), null);
        }
    });

    LinearLayout lay = (LinearLayout) v.findViewById(R.id.item_layout);
    if (tmp.getIsRead() != 0)
        lay.setBackgroundColor(Color.GRAY);
    else lay.setBackgroundColor(Color.YELLOW);
}

private ContentValues getContValues(News tmp)
{
    ContentValues cv = new ContentValues();
    cv.put("_id", tmp.getGuid());
    cv.put("title", tmp.getName());
    cv.put("pubdate", tmp.getPubdate());
    cv.put("description", tmp.getDescr());
    cv.put("isRead", tmp.getIsRead());
    cv.put("isFav", tmp.getIsFav());
    return cv;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v = mLayoutInflater.inflate(R.layout.list_item, parent, false);
    return v;
}

private String convertLongToDate(long value)
{
    Locale l = new Locale("ru");
    //if (l.getCountry())
    Date d = new Date(value);
    SimpleDateFormat df = new SimpleDateFormat("d M", l);
    return new String(df.format(d));                
}

}

4

1 回答 1

0

只需进行以下调整:

final ImageView fav_img = (ImageView) v.findViewById(R.id.image);
    Boolean is_favourite = false;
    if (fav_img != null)
    {
        if (tmp.getIsFav() != 0) {
            fav_img.setImageResource(R.drawable.fav);
        }
        else fav_img.setImageResource(R.drawable.unfav);
    }

    fav_img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (is_favourite) // if your row is already favourite
            {
               is_favourite=false;
                fav_img.setImageResource(R.drawable.unfav);
            }
            else 
            {
                is_favourite= true;
                fav_img.setImageResource(R.drawable.fav);
            }
            //Log.d("fav:", Integer.toString(tmp.getIsFav()) + " id: " + Integer.toString(tmp.getGuid()));
        //  db.UpdateItem(db.getWritableDatabase(), tmp);
        //  getLoaderManager().restartLoader(0, null, mCallbacks);
            //Working wrong!
            ContentValues cv = getContValues(tmp);
            loader.update(dbHelper.DATABASE_NAME, cv, dbHelper.UID + "=" + cv.getAsInteger("_id"), null);
        }
    });
于 2013-09-04T11:06:47.663 回答