Search code examples
androidlistviewbaseadaptersimpleadapter

Refresh ListView after user interaction


I'm struggling with updating my ListView after the user have deleted a row from the source-SQLite table.

I have searched for several days for a solution and discovered several places that recommend that i use notifyDataSetChanged() but i can't seem to figure out where to put it and how to use it.

I'm trying to use an BaseAdapter, but i'm not sure after looking at the LogCat that this is done correctly.

This is the code i'm using:

ArrayList<HashMap<String,String>> results = new ArrayList<HashMap<String,String>>(); 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    openAndQueryFav();
    displayResultList();


    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {

      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
              HashMap<String, String> item = (HashMap<String, String>) parent.getAdapter().getItem(position);
              String Start = item.get("Start").toString();
              String Name = item.get("Name").toString();
              try{
                  DBAdapter dbHelper = new DBAdapter(getBaseContext());
                  newDB = dbHelper.getWritableDatabase();
                  Cursor favorite = newDB.rawQuery("SELECT Start FROM " +
                            tableFav + " WHERE Start='" + Start + "'", null);
                  favorite.moveToFirst();

                      if(favorite.getCount() > 0){
                          newDB.execSQL("DELETE FROM " +
                                    tableFav + " WHERE start='" + Start + "'");
                          HeaderViewListAdapter adapterwrap = (HeaderViewListAdapter) parent.getAdapter(); 
                          BaseAdapter adapter = (BaseAdapter) adapterwrap.getWrappedAdapter();

                          adapter.notifyDataSetChanged();


                      }else{

                      }
              }catch(Exception e){

                  Log.d("Events", "catch, exception." + e.toString());
              }finally{

                  newDB.close();
              }
            }
      });

}



private void displayResultList() {

    ListAdapter adapter = new SimpleAdapter(
            this,
            results,
            R.layout.list,
            new String[] {"Start","Name","date"},
            new int[] {R.id.item_id,R.id.item_name,R.id.item_date}
            );
    setListAdapter(adapter);

    }

I must be doing something terrible wrong! Because when i try to run it i get this in the LogCat:

02-14 00:28:54.777: D/Events(9172): catch, exception.java.lang.ClassCastException: android.widget.SimpleAdapter

And if i try to put the:

HeaderViewListAdapter adapterwrap = (HeaderViewListAdapter) parent.getAdapter(); 
BaseAdapter adapter = (BaseAdapter) adapterwrap.getWrappedAdapter();

outside the try {} this error is in LogCat:

02-13 23:04:53.107: E/AndroidRuntime(9066): FATAL EXCEPTION: main
02-13 23:04:53.107: E/AndroidRuntime(9066): java.lang.ClassCastException: android.widget.SimpleAdapter
02-13 23:04:53.107: E/AndroidRuntime(9066):     at com.myapplication.Favorite$1.onItemClick(Favoritter.java:74)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.widget.ListView.performItemClick(ListView.java:3382)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.os.Handler.handleCallback(Handler.java:587)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.os.Looper.loop(Looper.java:144)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at android.app.ActivityThread.main(ActivityThread.java:4937)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at java.lang.reflect.Method.invoke(Method.java:521)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-13 23:04:53.107: E/AndroidRuntime(9066):     at dalvik.system.NativeStart.main(Native Method)

Can someone point me in the right direction, to get my listview updated after the task is performed?

Thanks!


Solution

  • I found an solution to my problem.

    Found a working example that i used to understand the problem.

    The main difference is that i use 'requery()' on the cursor after i have made the changes to the database that i wanted.

    This is the code after i did the changes that were required:

    newDB = new DBAdapter(this.getApplicationContext());
    
            c = newDB
            .getWritableDatabase()
            .rawQuery("SELECT _ID, Start, Name, Time, Fav FROM " +
                    tableName + " WHERE Fav='1'" , null);
            ListAdapter adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.list, c,
                    new String[] {"Start","Name","Time"},
                    new int[] {R.id.item_id,R.id.item_name,R.id.item_time}
                    );
            setListAdapter(adapter);
    
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);
    
            lv.setOnItemClickListener(new OnItemClickListener() {
    
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                    String Start = c.getString(c.getColumnIndex("Start"));
                      try{
                          newDB.getWritableDatabase().execSQL("UPDATE " +
                                            tableName + " SET Fav='0' WHERE Start='" + Start + "'");
                          c.requery();
    
                      }catch(Exception e){
    
                          Log.d("Events", "catch, exception." + e.toString());
                      }finally{
    
                      }
                    }
              });
    

    Tnx to you @Simon for trying to help me along!