我的CursorAdapter数据更新有问题 当我按下listview行项目的“star”img时,我需要更改DB列“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));
}
}
答案 0 :(得分: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);
}
});