2012-02-15 187 views
0

对不起,我英文不好/解释我是一个瑞士后不刷新......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); 
} 
+0

adapter.notifydatachangerd()? – 2012-02-15 09:47:35

+0

这个问题可能也是有趣的:[http://stackoverflow.com/questions/9060771/getting-sqlitecursorloader-to-observe-data-changes] – CjS 2012-02-15 09:48:50

回答

3

删除项目后,需要调用适配器的notifyDataSetChanged()方法刷新列表内容

fillData在活动标志似乎有点suspecious,我想这应该是如下:

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

好的,我会尝试一个。在我的其他(3个活动)我不使用notifyDataSetChanged()那里它的工作;) – 2012-02-15 09:51:16

+0

它不工作。同样phenomen ...'notes.notifyDataSetChanged();' – 2012-02-15 10:00:14

+0

我希望,有可能是这些活动:) – waqaslam 2012-02-15 10:00:48