抱歉我的英语不好/解释我是瑞士人......
当我删除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);
}
答案 0 :(得分:3)
删除项目后,需要调用适配器的notifyDataSetChanged()
方法刷新列表内容
fillData似乎有点可疑,我想它应该如下:
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 {
float weight = mCursor.getFloat(intWeight);
float mark = mCursor.getFloat(intMark);
zahl1 = zahl1 + (weight * mark);
zahl2 = zahl2 + weight;
} while (mCursor.moveToNext());
mCursor.moveToFirst(); //bringing cursor to first place
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);
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);
}