對不起,我英文不好/解釋我是一個瑞士後不刷新......ListView中刪除項目
當我刪除我的ListView一個項目,它是不是清爽。所以我點擊「Löschen」(英文「刪除」)。之後,該項目已在數據庫中刪除,但ListView未刷新項目。所以這個項目和我一樣長,直到我回到前一個站點並返回到ListView。 我能做什麼直接刷新它?
活動 「標誌」:在諮詢
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long id = getListAdapter().getItemId(info.position); /*what item was selected is ListView*/
switch (item.getItemId()) {
case CONTEXT_MENU_DELETE_ITEM:
mDbHelper.open_database_rw();
String id_delete = Long.toString(id);
try{
delItem(id_delete);
Toast.makeText(this, "Gelöscht!", Toast.LENGTH_SHORT).show();
fillData();
registerForContextMenu(getListView());
}
catch(SQLiteException e){
Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
}
return(true);
case CONTEXT_MENU_EDIT_ITEM:
try{
mDbHelper.open_database_rw();
id_edit = Long.toString(id);
Cursor cursor = db.query("tbl_marks", new String[] {"name", "mark", "gewicht"},
"_id="+id_edit, null, null, null, null);
startManagingCursor(cursor);
cursor.moveToFirst();
int name_mark = cursor.getColumnIndex("name");
name = cursor.getString(name_mark);
int mark_mark = cursor.getColumnIndex("mark");
mark = cursor.getString(mark_mark);
int mark_gewicht_int = cursor.getColumnIndex("gewicht");
gewicht_mark = cursor.getString(mark_gewicht_int);
gewicht_mark_int = Integer.parseInt(gewicht_mark);
removeDialog(EDIT_MARK_DIALOG);
showDialog(EDIT_MARK_DIALOG);
registerForContextMenu(getListView());
fillData();
}
catch(SQLiteException e){
Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
}
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void fillData() {
Bundle extras = getIntent().getExtras();
String txt_sub_id = extras.getString("IDFach");
int test = Integer.parseInt(txt_sub_id);
Cursor mCursor = db.rawQuery("SELECT _id, subid, name, mark, gewicht, datum FROM tbl_marks WHERE subid = '"+test+"';", null);
startManagingCursor(mCursor);
if (mCursor != null && mCursor.moveToFirst()) {
zahl1 = 0;
zahl2 = 0;
int intMark = mCursor.getColumnIndex("mark");
int intWeight = mCursor.getColumnIndex("gewicht");
do {
String[] from = new String[] { dbHelper.MARKS_ROW_NAME, dbHelper.MARKS_ROW_MARK, dbHelper.MARKS_ROW_GEWICHT, dbHelper.MARKS_ROW_DATUM};
int[] to = new int[] {R.id.txt_marks_row, R.id.txt_note, R.id.txt_gewicht, R.id.txt_datum};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_marks, mCursor, from, to);
setListAdapter(notes);
float weight = mCursor.getFloat(intWeight);
float mark = mCursor.getFloat(intMark);
zahl1 = zahl1 + (weight * mark);
zahl2 = zahl2 + weight;
} while (mCursor.moveToNext());
txt_durchschnitt = (TextView)findViewById(R.id.txt_durchschnitt);
result = zahl1/zahl2;
float result_neu = (float) (Math.round(result * 100)/100.0);
txt_durchschnitt.setText(""+result_neu);
}
}
謝謝!
try{
Log.d("Before Deleting the item...", "Log Before Delete");
delItem(id_delete);
Log.d("After Deleting the item...", "Log After Delete");
Toast.makeText(this, "Gelöscht!", Toast.LENGTH_SHORT).show();
Log.d("Before fill the list...", "Log befor fill the list");
fillData();
Log.d("Before fill the list...", "Log befor fill the list");
registerForContextMenu(getListView());
}
下面是日誌:
02-15 11:15:36.261: W/ActivityThread(7726): Application test.marco.notenha is waiting for the debugger on port 8100...
02-15 11:15:36.269: I/System.out(7726): Sending WAIT chunk
02-15 11:15:36.277: I/dalvikvm(7726): Debugger is active
02-15 11:15:36.468: I/System.out(7726): Debugger has connected
02-15 11:15:36.468: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:36.671: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:36.875: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:37.074: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:37.277: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:37.476: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:37.679: I/System.out(7726): waiting for debugger to settle...
02-15 11:15:37.878: I/System.out(7726): debugger has settled (1430)
02-15 11:15:38.011: D/dalvikvm(7726): GC_EXTERNAL_ALLOC freed 45K, 50% free 2695K/5379K, external 0K/0K, paused 31ms
02-15 11:15:38.070: D/dalvikvm(7726): GC_EXTERNAL_ALLOC freed 4K, 50% free 2699K/5379K, external 13K/523K, paused 26ms
02-15 11:15:39.500: D/dalvikvm(7726): GC_EXTERNAL_ALLOC freed 13K, 50% free 2727K/5379K, external 4412K/5218K, paused 18ms
02-15 11:15:45.484: D/dalvikvm(7726): GC_CONCURRENT freed 157K, 48% free 2951K/5575K, external 9362K/10626K, paused 1ms+2ms
02-15 11:15:49.046: W/KeyCharacterMap(7726): Can't open keycharmap file
02-15 11:15:49.046: W/KeyCharacterMap(7726): Error loading keycharmap file '/system/usr/keychars/aries-keypad.kcm.bin'. hw.keyboards.0.devname='aries-keypad'
02-15 11:15:49.046: W/KeyCharacterMap(7726): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-15 11:15:53.277: D/Before Deleting the item...(7726): Log Before Delete
02-15 11:15:53.289: D/After Deleting the item...(7726): Log After Delete
02-15 11:15:53.304: D/Before fill the list...(7726): Log befor fill the list
02-15 11:15:53.308: D/Before fill the list...(7726): Log befor fill the list
這裏它的工作(我不知道爲什麼它,但在上面的代碼,它不工作) 活動「功課」:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long id = getListAdapter().getItemId(info.position); /*what item was selected is ListView*/
switch (item.getItemId()) {
case CONTEXT_MENU_DELETE_ITEM:
String id_delete = Long.toString(id);
try{
delItem(id_delete);
Toast.makeText(this, "Gelöscht!", Toast.LENGTH_SHORT).show();
fillData();
registerForContextMenu(getListView());
}
catch(SQLiteException e){
Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
}
return(true);
case CONTEXT_MENU_EDIT_ITEM:
try{
id_edit = Long.toString(id);
mDbHelper.open_database_rw();
Cursor cursor = db.query("tbl_homework", new String[] {"hw"},
"_id="+id_edit, null, null, null, null);
startManagingCursor(cursor);
cursor.moveToFirst();
int column = cursor.getColumnIndex("hw");
hw = cursor.getString(column);
removeDialog(EDIT_HW_DIALOG);
showDialog(EDIT_HW_DIALOG);
fillData();
}
catch(SQLiteException e){
Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
}
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void fillData() {
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { dbHelper.HOMEWORK_ROW_HW, dbHelper.HOMEWORK_ROW_HWDATE};
int[] to = new int[] {R.id.txt_notes_row, R.id.txt_homework};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_homework, c, from, to);
setListAdapter(notes);
}
adapter.notifydatachangerd()? – 2012-02-15 09:47:35
這個問題可能也是有趣的:[http://stackoverflow.com/questions/9060771/getting-sqlitecursorloader-to-observe-data-changes] – CjS 2012-02-15 09:48:50