0

I needed to make a recently opened module for my app. I'm using a viewpager with 3 fragments, each one of those has a listview on it. The 1st listview that when clicked must store the clicked item on a listview on a different fragment. I was planning on using a different table, but i think that there is something much better, some sort of a temp memory.

1 Answers1

0

It sounds like that you don't have a lot of data to save. You can consider using the SharedPreferences.

Depending on what the item is in your listview, for example, if they are strings, you can store and retrieve them like this:

Saving:

string selectedItem = ...; 
SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(this);
manager.edit().putString("SelectedItem", selectedItem).apply();

Retrieving:

SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(this);
String selectedItem = manager.getString("SelectedItem", "");
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • But every time you only saved one that is selected right? – Yuchen Sep 20 '15 at 15:42
  • yup, one at a time. one fragment will load all of it. and one at a time when clicked. –  Sep 20 '15 at 15:43
  • Then SharedPreferences is a right solution for you. Basically, it will also save the data to database. But instead creating a database table yourself, SharedPreferences do that for you. In your case, since you only one value to save, I don't see the value of creating a different table. – Yuchen Sep 20 '15 at 15:45
  • "It will also save the data to database"? I have my own database, or i just misunderstood your statement or saving the data in the database? –  Sep 20 '15 at 15:48
  • @RenzRazon, it is okay that you have your own database. But this is a small database that is managed by Android separately for simple tasks. What I mean by saying "It will also save the data to database" is that you will still be able to retrieve the data when you close the app and reopen it. – Yuchen Sep 20 '15 at 15:52
  • 1
    I see. :D thanks, Really helped a lot, i'll work on this. (y) –  Sep 20 '15 at 15:54