1

I have an Android app with a listview that updates the listview when the user presses a button. The app has two actionbar tabs. When I switch between the tabs, rotate the screen, or press back to exit out of the activity, all the data in the listview disappears. I need the data to remain. I know that at least with the orientation change I can save the data in onSaveInstanceState(). So I save a String[] in savedInstanceState, and retrieve the String[] in onCreateView(). This seems to work fine, it reads the data properly. Then I attempt to update the listview. This also seems to read the data and work properly, only the listview does not update and instead remains blank. My code for this is:

if (savedInstanceState != null) {
    lvArray = savedInstanceState.getStringArray("lvArray");
    setupListView();
} else {
    lvArray= new String[LISTVIEW_LIMIT];
    for (int i = 0; i < LISTVIEW_LIMIT; i++) {
         lvArray[i] = " ";
    }
    setupListView();
}

 private void setupListView() {
    if (listview.getAdapter() == null) {
        ArrayAdapter<String> listviewAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, lvArray);
        listview.setAdapter(listviewAdapter);
    }
    registerForContextMenu(listview);
}

So as I said, I go through this, it reads the data properly, it sets up an ArrayAdapter of the data, and sets the adapter for the the listview. It all seems to work, only it doesn't actually update it. I know all the data's there but it's still not working?

Also, I have the ability to set the listview to different sizes (by LISTVIEW_LIMIT). This works fine, except it also removes all the data already in the listview. How can I keep the data instead of recreating the array when the limit changes?

And what would be the best way to save this in onPause() or onStop(), so that I could keep the listview up even if back is pressed or the tab is switched?

EDIT: Changed to use notifyDataSetChanged(), still not working:

    lvStopwatch = (ListView) view.findViewById(R.id.list_stopwatch);
    stopwatchTimes = new String[TOTAL_TIMES];
    for (int i = 0; i < TOTAL_TIMES; i++) {
        stopwatchTimes[i] = " ";
    }
    setupListView();

    if (savedInstanceState != null) {
        stopwatchRunning = savedInstanceState.getBoolean("stopwatchRunning", false);
        stopwatchTimes = savedInstanceState.getStringArray("stopwatchTimes");
        stopwatchAdapter.notifyDataSetChanged();
    } else {
        stopwatchRunning = prefs.getBoolean("stopwatchRunning", false);
        Log.d("savedPASS", String.valueOf(stopwatchRunning));

    }

   private void setupListView() {
      if (lvStopwatch.getAdapter() == null) {

         stopwatchAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, stopwatchTimes);
         lvStopwatch.setAdapter(stopwatchAdapter);
      }
      registerForContextMenu(lvStopwatch);
   }
NSchock
  • 253
  • 1
  • 7
  • 17
  • possible duplicate of [How to refresh Android listview?](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) – KV Prajapati Sep 27 '12 at 04:06

3 Answers3

0

try this

adapter.notifyDataSetChanged();
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Unfortunately this doesn't seem to work either. It just does the same thing. – NSchock Sep 27 '12 at 03:56
  • it will work, after adding data to the array use this code it will refresh listview.it's working for me. – RajaReddy PolamReddy Sep 27 '12 at 03:57
  • Where do I put it? I tried immediately after retrieving the data and I tried outside of the if statement for savedInstanceState but it still didn't do anything. – NSchock Sep 27 '12 at 04:11
  • I am calling it immediately after the data change, and it's not working. – NSchock Sep 27 '12 at 04:24
  • i think you are using nofifydataset() after listviewset() method, use it before setlistview() and try like this once. – RajaReddy PolamReddy Sep 27 '12 at 04:42
  • I just tried that but it caused the app to crash. setupListView() initializes the adapter, so if I try to notify the data set before I init the adapter it's just gonna because the adapter is null. – NSchock Sep 27 '12 at 04:53
  • initializes the adapter in the onCreate() method and make it global – RajaReddy PolamReddy Sep 27 '12 at 04:55
  • Okay I did that but...at this point the only thing setupListView() does is set the adapter to the listview and register a context menu. Calling it after notifydataset doesn't seem to do anything differently. – NSchock Sep 27 '12 at 05:02
  • Actually when use that was working for me fine, i am able refreshing the data. but i am not getting where is the problem in your code.. – RajaReddy PolamReddy Sep 27 '12 at 05:04
  • I don't understand the problem either. It seems like everything should work, I know the code is right. Perhaps it has something to do with being in the onCreateView() method of a fragment? But I don't know why that would prevent it from working. – NSchock Sep 27 '12 at 05:08
  • Yes. Should it matter though? – NSchock Sep 27 '12 at 22:33
  • I messed around with the app a bit and found out that it actually is saving the values to the listview. They're all there, but they don't show up for some reason? I can still click on them and use them and stuff, they just don't appear. – NSchock Sep 28 '12 at 05:13
0

Keep listview data save in some data structure.
I find HashMaps best for this.
Call Sales adapter again when you want to populate.

adapter=new MyAdapter(dataHashMap);
listView.setAdapter(adapter);
WooCaSh
  • 5,180
  • 5
  • 36
  • 54
RohitAneja
  • 966
  • 12
  • 15
0

Found the solution. For some reason it doesn't display the values properly when restoring them in the onCreateView() method. I restored them in the onCreate() method of the fragment instead . I didn't really change much other than moving the saveInstanceState check into the onCreate() method, but now it works.

NSchock
  • 253
  • 1
  • 7
  • 17