我有一个 listfragment 显示来自数据库的列表。除了一个我似乎无法清理的小物品外,我的一切都正常工作。列表视图看起来像这样。
-----------------------------------------------------
text text text | some more text | delete row button |
-----------------------------------------------------
我的问题:当用户按下删除行按钮时,该行将从数据库中删除,但不会从屏幕上删除,直到活动停止并再次启动。
我知道这notifyDataSetChanged()
是更新这些列表的首选方法,但它似乎没有做任何事情......无论如何,下面是我notifyDataSetChanged()
调用光标适配器的代码。
public class CalcCursorAdapter extends SimpleCursorAdapter{
private Context mContext;
private ListView mListView;
private int mLayout;
private Cursor mcursor;
protected static class ViewHolder {
protected TextView text;
protected ImageButton button;
private int position;
}
@SuppressWarnings("deprecation")
public CalcCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mcursor = c;
//mListView = .getListView();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView summary = (TextView)view.findViewById(R.id.calctvPrice);
TextView savings = (TextView)view.findViewById(R.id.calctvSavings);
TextView percentOff = (TextView)view.findViewById(R.id.calctvpercentOff);
summary.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcFinalPrice")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.calc_list_item, parent, false);
holder.button = (ImageButton) v.findViewById(R.id.qcbtnDelete);
holder.button.setOnClickListener(deleteButton);
holder.position = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
bindView(v, context, cursor);
v.setTag(holder);
return v;
}
private OnClickListener deleteButton = new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v){
View view = (View) v.getParent();
ViewHolder holder = (ViewHolder) view.getTag();
int position = holder.position;
DbHelper mDbHelper;
mDbHelper = new DbHelper(mContext);
mDbHelper.open();
mDbHelper.deleteCalc(position);
mDbHelper.close();
String test = Integer.toString(position);
Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
//ListView list = getListView();
}
};
public long qcItemId(int position) {
return position;
}
}
谢谢您的帮助。
更新:
我的适配器中的新按钮代码:
private OnClickListener deleteButton = new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v){
v.invalidate();
View view = (View) v.getParent();
view.invalidate();
ViewHolder holder = (ViewHolder) view.getTag();
int position = holder.position;
DbHelper mDbHelper;
mDbHelper = new DbHelper(mContext);
mDbHelper.open();
mDbHelper.deleteCalc(position);
mDbHelper.close();
String test = Integer.toString(position);
Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show();
Cursor newCursor = getCursor();
changeCursor(newCursor);
notifyDataSetChanged();
//ListView list = getListView();
}
};