2

i am working on an app to develop my knowledge so far i have created a calendar app which user can make appointment when clicking on a date. i then have a delete button which opens all the appointments created in a fresh activity, there i have: a list view which displays all appointments a textview which displays the selected appointment (selected by the user to delete) and a button remove. the remove button removes all the items from the sqlite but i can only see this working when the application is restarted i know i am missing something else in my code.. how do i clear the listview at the same time any help would be much appreciated

my code:

delete all method

public void deleteAll() {

        db.delete(TABLE_NAME, null, null);
    }

button code:

public void onClick(View v) {
    switch(v.getId()){

    case R.id.removeBtn:

        dm.deleteAll();

        break;
    }

thank you }

Tacit
  • 890
  • 6
  • 17
  • 42
  • 1
    you have to requery the cursor and update your listview with the new one – zapl Apr 19 '12 at 22:02
  • Ah so by "see this working" you mean you don't see the items removed? Or have you checked the database and `deleteAll();` is failing? – nuala Apr 19 '12 at 22:04
  • i tried this {c.requery();} but it keeps on saying Cursor is deprecated – Tacit Apr 19 '12 at 22:06
  • @yoshi it delete the database but doesn't update the screen only if/when i restart it – Tacit Apr 19 '12 at 22:07

3 Answers3

3

Maybe not the most elegant, but I don't build my UI in onCreate(). Instead, in onCreate(), I call another method initializeUI(), and that is where I build my user interface. Then, anytime I do something that should be reflected in the interface (update the database or whatever), I just need to call my method initializeUI() again.

Edit: Also look into notifyDataSetChanged() for your adapter as exampled here

Community
  • 1
  • 1
Mobius
  • 699
  • 4
  • 11
1

First check if your database is writable (getWritableDatabase)

Then check that your "TABLE_NAME" is in fact the table name.

Then you could try to use execSQL("delete from " + TABLE_NAME);

if that does not work, you could try to close the db.

finally try putting it in a transaction beginTransaction(); .. delete.. endTransaction();

mpx
  • 199
  • 2
  • 9
1

If you are actually changing the data from the Adapter used in the ListView, you could use requery() method on the cursor used to populate the Adapter. This should reflect your changes!

Caumons
  • 9,341
  • 14
  • 68
  • 82
  • i tried this c.requery(); but it keeps on saying Cursor is deprecated what do i use instead – Tacit Apr 20 '12 at 15:42