user5378946
user5378946

Reputation:

How to save data in Android and use it for future use without DB?

I have created a ListView and it can add data dynamically but whenever I restart the App the previous stored list is lost.

How can I save that list ?

Upvotes: 0

Views: 277

Answers (3)

Parth
Parth

Reputation: 137

If you need to persist large volume of data you should use SQLite database and it is best for this purpose. But you can also use xml to store your data, xml is slow then SQLite database.

You can refer this standard Storage options.

Upvotes: 0

Sedat Polat
Sedat Polat

Reputation: 1721

You can save them into client local via using android SharedPreferences

Or, you can write your own model. You should pass your object here;

public boolean writeYourObjectOnLocal(File dir, YourObject yourObject) {
    ObjectOutput output = null;
    OutputStream buffer = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(dir.toString() + File.separator + "myFile.dat");
        buffer = new BufferedOutputStream(fileOutputStream);
        output = new ObjectOutputStream(buffer);

        output.writeObject(yourObject);
        return true;
    } catch (Throwable e) {
        return false;
    } finally {
        try {
            output.close();
        } catch (Throwable e) {}
        try {
            buffer.close();
        } catch (Throwable e) {}
        try {
            fileOutputStream.close();
        } catch (Throwable e) {}
    }
}

You can Read your object;

public YourObject readYourObjectFromLocal(File dir) {
    ObjectInput input = null;
    BufferedInputStream buffer = null;
    FileInputStream fileInputStream = null;
    try {
        String fileName = dir.toString() + File.separator + "myFile.dat";
        fileInputStream = new FileInputStream(fileName);
        buffer = new BufferedInputStream(fileInputStream);

        input = new ObjectInputStream(buffer);
        return (YourObject)input;

    } catch (Throwable e) {
        return null;
    } finally {
        try {
            input.close();
        } catch (Throwable e) {
        }
        try {
            fileInputStream.close();
        } catch (Throwable e) {
        }
        try {
            buffer.close();
        } catch (Throwable e) {
        }
    }
}

Upvotes: 1

Vedran V
Vedran V

Reputation: 11

I would recommend you to use caching library like Reservoir. Check instructions how to use it on this link. https://github.com/anupcowkur/Reservoir

Be sure to allocate enough memory in your application class (size in bytes).

Example: Save data (Async):

// it can be any type of object (here is String)
List<String> strings = new ArrayList<String>();
strings.add("one");
strings.add("two");
strings.add("three");
Reservoir.putAsync("myListKey", strings, new ReservoirPutCallback() {
        @Override
        public void onSuccess() {
            //success
        }

        @Override
        public void onFailure(Exception e) {
            //error
        }
    });        

Example: Read saved data (Async):

Reservoir.getAsync("myListKey", new TypeToken<List<String>>() {}.getType(), 
    new ReservoirGetCallback<List<String>>() {
        @Override
        public void onSuccess(List<String> strings) {
            //success - set your list adapter and show those items
        }

        @Override
        public void onFailure(Exception e) {
            //error
        }
    }); 

Upvotes: 0

Related Questions