1

Here is the code:

@Override
public void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();

    Log.d("onResumeMethod", "It is called! :)");
}
}

I am using this to refresh list when anything is edited in the database. Unfortunately it is not working. Somewhere I read I should use requery but it is already deprecated. Any advice?

EDIT:

Tried already this, but did not help(unfortunately, because I was convinced it is gonna work):

@Override
public void onResume() {
    super.onResume();
    adapter = new ArrayAdapter<Friend>(this,
            android.R.layout.simple_list_item_1, list);
    this.getListView().setAdapter(adapter);
    this.getListView().invalidate();
}

I found another problem: If I use back button (on emulator) to go back to desktop and try to turn on an application by clicking on a menu item it shows me this:

03-19 15:43:50.153: E/AndroidRuntime(7374): FATAL EXCEPTION: main
03-19 15:43:50.153: E/AndroidRuntime(7374): java.lang.RuntimeException: Unable to start     
activity     
ComponentInfo{com.example.birthdayreminder/com.example.birthdayreminder.MainActivity}: 
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 
'android.R.id.list'

I faced this problem before so they adviced me to insert this to my xml:

 <ListView
 android:id="@android:id/list"
 android:layout_width="wrap_content"
 android:layout_height="match_parent" >
 </ListView>

2 Answers2

2

You need to get your data from database again. Then you can set the adapter again in your listview and call invalidate(), like the following method:

@Override
public void onResume() {
    super.onResume();

    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, db.getMyData());

    myListView.setAdapter(adapter);

    myListView.invalidate();
}

Where db.getMyData() is your method to get your data from the database, and myListView is your ListView.

Hope it helps!

Giacomoni
  • 1,468
  • 13
  • 18
  • Change android:id="@android:id/list" to android:id="@+id/list". You can check it on this post http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android – Giacomoni Mar 19 '14 at 16:19
  • Solved, you were right I just needed to get reference to my listView, I did it by doing this.getListView(); – Ondrej 'zatokar' Tokár Mar 19 '14 at 17:59
0

Override notifyDataSetChanged :

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();

    myArray.clear();
    /* load the data again */
    myArray = yourMethodToLoadData();
}
Kody
  • 1,154
  • 3
  • 14
  • 31