我要在单击后删除该行。它正在从数据库中删除行,但更改不会立即显示在屏幕上。关闭并重新打开该应用程序后,它会出现。
我已经使用notifyDataSetChanged()。
MainActivity.java
@Override
protected void onStart() {
super.onStart();
display();
}
public void display(){
final DbHelper mDbHelper = new DbHelper(this);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(" SELECT * FROM "+ Contract.Entry.TABLE_NAME, null);
ListView lv = (ListView)findViewById(R.id.tv);
final nCursorAdapter nc = new nCursorAdapter(this, cursor);
lv.setAdapter(nc);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
db.delete(Contract.Entry.TABLE_NAME, Contract.Entry.COL_ID + " = ?",
new String[] { String.valueOf(id) });
db.close();
nc.notifyDataSetChanged();
}catch (Exception e){
Toast.makeText(getApplicationContext(),""+e, Toast.LENGTH_SHORT).show();
}
}
});
lv.invalidateViews();
}
nCursorAdapter.java
public class nCursorAdapter extends CursorAdapter {
public nCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@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, Context context, Cursor cursor) {
TextView first_name = (TextView)view.findViewById(R.id.namef);
TextView last_name = (TextView)view.findViewById(R.id.namel);
TextView ph_num = (TextView)view.findViewById(R.id.number);
String fname = cursor.getString(cursor.getColumnIndex(Contract.Entry.COL_F_NAME));
String lname = cursor.getString(cursor.getColumnIndex(Contract.Entry.COL_L_NAME));
String phn = cursor.getString(cursor.getColumnIndex(Contract.Entry.COL_NUM));
first_name.setText(fname);
last_name.setText(lname);
ph_num.setText(phn);
}
}
答案 0 :(得分:0)
此方法将重新加载Activity
,而不会出现闪烁或动画
private void refresh() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
因此,删除行后,请调用此方法。