我正在通过调用delete()
方法删除该行。它从数据库中删除,但页面未更新。一旦我关闭应用程序,然后再次打开或转到其他页面然后返回,该行便从屏幕上消失了。
从数据库中删除一行以立即显示更新后的数据后,如何刷新页面?
mCursorAdapter.java
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
final TextView rowId = (TextView)view.findViewById(R.id.rowId);
final String rowNo = cursor.getString(cursor.getColumnIndex(Contract.Entry._ID));
rowId.setText(rowNo);
final TextView a = (TextView)view.findViewById(R.id.name);
int nameColumnIndex = cursor.getColumnIndex(Contract.Entry.COLUMN_F_NAME);
String name = cursor.getString(nameColumnIndex);
a.setText(name);
final CheckBox cb = (CheckBox)view.findViewById(R.id.c);
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(cb.isChecked()) {
a.setPaintFlags(a.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
a.setPaintFlags(a.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});
final ImageView img = (ImageView)view.findViewById(R.id.summary);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to DELETE the task?");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
delete();
//notifyDataSetChanged();
}
private void delete() {
DbHelper mDbHelper = new DbHelper(context);
SQLiteDatabase sqLiteDatabase = mDbHelper.getWritableDatabase();
sqLiteDatabase.delete(Contract.Entry.TABLE_NAME,Contract.Entry._ID + "=?",new String[]{rowNo});
//Toast.makeText(context,"Data removed from row " + rowNo, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context,"No Button Clicked",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}
答案 0 :(得分:0)
您已经删除了数据库中的记录,但是还必须更新类中的数据集,并在适配器上调用.notifyDataSetChanged()
。
您可以使用广播通知适配器,也可以/应该将delete方法移至适配器类。
类似这样的东西:
@Override
public void bindView(View view, final Context context, Cursor cursor) {
HERE YOU MAKE YOUR DIALOG
}
//mopve your DELETE method outside of bindview
private void delete() {
1.first delete record
2.update collection in class
3.invoke notifyDataSetChanged() on adapter
}