简体   繁体   English

删除项目后,ListView不刷新

[英]ListView doesn't refresh after deleting an item

Sorry for my bad English / explanation I'm a Swiss... 对不起,我的英语不好/解释我是瑞士人...

When I delete an item in my ListView, it is not refreshing. 当我删除ListView中的项目时,它没有刷新。 So I click on "Löschen" (in English "Delete"). 因此,我单击“Löschen”(英文为“ Delete”)。 After that the item has been deleted in database but the ListView didn't refresh the items. 之后,该项目已在数据库中删除,但ListView并未刷新该项目。 So the item is as long as there until I go back to a previous site and back again to the ListView. 因此,该项目就一直存在,直到我回到上一个站点并再次回到ListView。 What can I do for refreshing it directly? 如何直接刷新它?

Activity "marks": 活动“标记”:

    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);
    }
}

Thanks in advice! 感谢您的建议!

             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());
             }

Here are the logs: 以下是日志:

    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

HERE ITS WORKING (I don't know why it is but in the code above it's not working) Activity "homework": 在这里工作(我不知道为什么会这样,但是在上面的代码中却无法正常工作)活动“作业”:

    @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);
}

after deleting an item, you need to call adapter's notifyDataSetChanged() method for refreshing the list contents 删除项目后,您需要调用适配器的notifyDataSetChanged()方法刷新列表内容

fillData in your Activity Marks seems a little suspecious, i guess it should be as follow: 您的活动标记中的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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM