title: How to delete the last item of a listview and refresh the view correctly?
body: <p>"Gelöscht" means "Deleted" in English...
  "Löschen" means "delete" in English...</p>
  
  <p>Everything works in homework.java but not in the second part (mark.java). So why isn't it work in mark.java but here? It's like the same code...</p>
  
  <p>I have a ListView with homework in it like you can see: (homework.java)</p>
  
  <p><img src="https://i.stack.imgur.com/gcHj7.jpg" alt="Pic 1"></p>
  
  <p>When I delete the last item it refreshes correctly:</p>
  
  <p><img src="https://i.stack.imgur.com/JFSwT.jpg" alt="Pic 2">
  <img src="https://i.stack.imgur.com/iynu4.jpg" alt="Pic 3"></p>
  
  <p>What happens if "Löschen" has been touched... (CONTEXT_MENU_DELTE_ITEM)</p>
  
  <pre><code>    public boolean onContextItemSelected(MenuItem item) {
          //Adapterview definieren
          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
          long id = getListAdapter().getItemId(info.position);                            //Gibt die ID des gedrückten Items zurück
  
          switch (item.getItemId()) {
              case CONTEXT_MENU_DELETE_ITEM:                                              //Wenn die Auswahl auf CONTEXT_MENU_DELETE_ITEM fiel
                  String id_delete = Long.toString(id);                                   //Die Item ID wird dem String zugewiesen
                  try {
                      delItem(id_delete);                                                 //Löschen des Items (siehe Funktionen) mit der ID id_delete
                      Toast.makeText(this, "Gelöscht!", Toast.LENGTH_SHORT).show();       //Anzeige "Gelöscht!"
                      fillData();                                                         //ListView wieder mit DB Daten füllen
                      registerForContextMenu(getListView());                              //ContextMenu registrieren
                  }
                  catch(SQLiteException e){
                      Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show(); //Bei einem Fehler: Nicht gelöscht...
                  }
                  return(true);
  
                  case CONTEXT_MENU_EDIT_ITEM:                                            //Wenn die Auswahl auf CONTEXT_MENU_EDIT_ITEM fiel
                      try {
                      id_edit = Long.toString(id);                                        //Die Item ID wird dem String zugewiesen
                      mDbHelper.open_database_rw();                                       //Datenbank wird geöffnet
  
                      Cursor cursor = db.query("tbl_homework", new String[] {"hw"},       //Dem Cursor wird der Eintrag "hw" in der Tabelle tbl_homework zugewiesen, welcher die _id vom Item hat
                                      "_id="+id_edit, null, null, null, null);
                      startManagingCursor(cursor);                                        //Managed den Cursor                
  
                      cursor.moveToFirst();                                               //Cursor an den Anfang setzen
                      int column = cursor.getColumnIndex("hw");                           //Der SpaltenIndex in column schreiben
                      hw = cursor.getString(column);                                      //String in hw schreiben (wird bei den Dialogen wieder benötigt!)
                      showDialog(EDIT_HW_DIALOG);                                         //EDIT_HW_DIALOG Dialog aufrufen, siehe Dialoge
  
                      fillData();                                                         //ListView mit DB Einträgen füllen
  
                      }
                      catch(SQLiteException e){
                          Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show(); //Meldung: Nicht gelöscht, bei Fehler
                      }
                      return(true);
          }
        return(super.onOptionsItemSelected(item));                                        //Selektierte Item wird zurückgegeben
      }
  </code></pre>
  
  <p>Funkctions....</p>
  
  <pre><code>    /** AlertDialog: Fehler-Nachrichten-Funktion */
      private void fillData() {
          c = mDbHelper.fetchAllNotes();                                                          //Cursor c mit fetchAllNotes von dbHelper.java initialisieren
          startManagingCursor(c);                                                                         //Managed den Cursor
          String[] from = new String[] { dbHelper.HOMEWORK_ROW_HW, dbHelper.HOMEWORK_ROW_HWDATE};         //Datenzuweisung dem String from
          int[] to = new int[] {R.id.txt_notes_row, R.id.txt_homework};                                   //Datenzuweisung dem Integer to
  
          //ArrayAdapter deklarieren und anzeigen
          SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_homework, c, from, to);
          setListAdapter(notes);
      }
  
      /** delItem: Funktion zum Löschen von einem Datensatz */
      public void delItem(String item){
          db.delete("tbl_homework", "_id="+item, null);           //Datensatz löschen
      }
  </code></pre>
  
  <p>But in my second ListView I have some marks in it. When I try to delete the last one there
  it has been deleted in database (I checked that) but didn't refresh the ListView like in homework. I always has to restart my activity first. But in homework I don't have to... 
  My question is why and how can I fix it? (marks.java)</p>
  
  <p><img src="https://i.stack.imgur.com/jZ1GH.jpg" alt="Pic 4">
  <img src="https://i.stack.imgur.com/7wsMo.jpg" alt="Pic 5">
  <img src="https://i.stack.imgur.com/mgcar.jpg" alt="Pic 6"></p>
  
  <p>What happens if "Löschen" has been touched... (CONTEXT_MENU_DELETE_ITEM)</p>
  
  <pre><code>@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:
                   mDbHelper.open_database_rw();
                   String id_delete = Long.toString(id);
                   try{
                       delItem(id_delete);
                       Toast.makeText(this, "Note 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 = 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();
                       cursor.close();
                   }
                   catch(SQLiteException e){
                       Toast.makeText(this, "Nicht gelöscht!", Toast.LENGTH_SHORT).show();
                   }
                   return(true);
       }
   return(super.onOptionsItemSelected(item));
   } 
  </code></pre>
  
  <p>Functions...</p>
  
  <pre><code>private void fillData() {
      Bundle extras = getIntent().getExtras();
      String txt_sub_id = extras.getString("IDFach");
      int test = Integer.parseInt(txt_sub_id);
  
      cursor = db.rawQuery("SELECT _id, subid, name, mark, gewicht, datum FROM tbl_marks WHERE subid = '"+test+"';", null);
      startManagingCursor(cursor);
      if (cursor != null &amp;&amp; cursor.moveToFirst()) {
           zahl1 = 0;
           zahl2 = 0;
           int intMark = cursor.getColumnIndex("mark");
           int intWeight = cursor.getColumnIndex("gewicht");
            do {
                float weight = cursor.getFloat(intWeight);
                float mark = cursor.getFloat(intMark);
  
                zahl1 = zahl1 + (weight * mark);
                zahl2 = zahl2 + weight;
            } while (cursor.moveToNext());
  
            cursor.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};
  
  
            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);
            SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.show_marks, cursor, from, to);
            setListAdapter(notes);
      }
  }
  
  public void delItem(String item){
      db.delete("tbl_marks", "_id="+item, null);
  }
  </code></pre>