0

I am new to Android App development, working on an android app which populate a list of numbers, in a listview dynamically, depending on the choice of the user, but, the moment user closes the App, the items in the listview are lost. How can I maintain the state of the listview?

Examples with code would be highly appreciated.

When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help.

User11012
  • 107
  • 8
  • Have a look at this question from SO http://stackoverflow.com/questions/23024831/android-shared-preferences-example this will guide you through – MDMalik Feb 02 '15 at 19:12
  • @MDMalik: Thanks for your response, but, what you had suggested works well to maintain the session of an application whereas I need the to maintain the items of Listview. When I open Activity A, it allows users to add friends, and this friend list is shown in the form of items of listview in the same Activity, however, when I move to Activity B, and then come back to Activity A, this friend list disappears. I need to make sure that this friend list should not be lost while moving between activities. Please help. – User11012 Feb 04 '15 at 13:12

2 Answers2

0

I think that for your purpose there are 3 main methods, i'll explain them from the easier to the most difficult (in my opinion).

Text File

A way to do this is to create two methods in a class: one has to create the text file in the storage if it isn't created before and read that, the other has to append a String to a StringBuilder and write it on the previous text file.

For this method you need the uses-permission of reading and writing to storage.

This link can help you: http://developer.android.com/training/basics/data-storage/files.html

JSON (also XML)

With JSON file you can create a list of objects with your data that you can serialize when you update the list and deserialize when you want to read it. For this purpose you have to study JavaScript syntax or, at least, JSON one.

SQLite Database

Android SDK incorporate a class named SQLiteOpenHelper that you can extend to create a database inside your app.

This link can help you: http://developer.android.com/training/basics/data-storage/databases.html


There are also references saving methods but i think that aren't right for your purpose, they work betters to save something like preferences or single data like last login informations.

Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70
  • Thanks a lot for your response. It was of great help, but, didn't resolve my issue. I need to maintain the session state of items of Listview. The one you gave works well with textview, but, didn't help me with items of Listview. – User11012 Feb 04 '15 at 13:06
  • @User11012 On JSON you can store arrays and lists, on txt file you can save array or list objects separated by \n with properties separates by semicolons, with database you can store properties of an object, so why it didn't work? – Giorgio Antonioli Feb 04 '15 at 18:59
  • I am actually not sure when the listview is resumed, how would I update the UI of listview with the data of the DB. Could you please guide me on this? – User11012 Feb 05 '15 at 12:58
0

I went through your comment. I would personally suggest using SQLiteOpenHelper

You might need to understand the use of SQLite, hence the tutorial

Simple Flow. On your Activity 1 where person Add Friends save it to DB
Then refresh the List from the DB. So when you move to Activity 2 and come back again to Activity 1 your List will refresh from DB. Hence no loss of data as you want.

EDIT

As user wanted to know how to use the ListView with DB.

Following are my suggestion

MDMalik
  • 3,951
  • 2
  • 25
  • 39