0

I am trying to save some user data internally. Ideally, I would like to save

String[] text;
String name;
String file_name;

Together in one (for lack of a better term) package, and then use all saves name's with the other data to populate another activities listView, where I can load the saved information. Here is the code I am trying to use to save the information:

Button fileName;
fileName = (Button) findViewById(R.id.save_text);

fileName.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            outputStream = openFileOutput(file_name, Context.MODE_APPEND);
            ObjectOutputStream phone_save = new ObjectOutputStream(outputStream);
            phone_save.writeObject(name);
            phone_save.writeObject(text);
            Log.i("Save", "Files saved");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

Then this is the code that tries to get that saved information(for now I just have it trying to set the text of a text View, not a listView yet)

Button load;
TextView load_text
load = (Button) findViewById(R.id.load);

load.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            fin = openFileInput(file_name);
            ObjectInputStream ois = new ObjectInputStream(fin);
            String[] Loaded_Text = (String[]) ois.readObject();
            ois.close();
            load_text.setText(Html.fromHtml(Arrays.toString(Loaded_Text)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

I am having a problem saving and loading the data, no log statement appears when I push the filename button, and nothing appears in the textView when I push the load Button. Essentially none of it works, and I have absolutely no clue how to fix it. I'm sure this code is a mess, but this is really my first time working with storage. Thanks everyone!

nicopico
  • 3,606
  • 1
  • 28
  • 30
Ethan
  • 225
  • 2
  • 13
  • Did you consider using SharedPreferences? I think that would be a lot simpler than writing and reading files. – nasch Jan 24 '15 at 19:42
  • @nasch But wouldn't that mean I would have to create anew SharedPreferences for every piece of data the user creates? – Ethan Jan 24 '15 at 21:46
  • As long as each piece of data can have a unique key, they can all go in the same one. https://developer.android.com/guide/topics/data/data-storage.html#pref – nasch Jan 24 '15 at 21:58
  • @nasch And I could assign this unique key by just setting each file with a String fileName and an int? And could I use these shared preferences to populate a listView in another activity? – Ethan Jan 24 '15 at 22:01
  • I'm not sure what you mean - did you read up on how SharedPreferences work? You store key-value pairs, so any piece of data needs a unique string to identify it. `prefs.putString("key1",stringValue); prefs.putString("key2", anotherStringValue);` You would just need to convert your string array to and from a string. – nasch Jan 25 '15 at 00:05
  • @nasch Ok thats the route I will go with this. Thanks!' – Ethan Jan 25 '15 at 00:13

2 Answers2

1

Posting as an answer you can accept.

Did you consider using SharedPreferences? I think that would be a lot simpler than writing and reading files. You store key-value pairs, so any piece of data needs a unique string to identify it.

prefs.putString("key1",stringValue); 
prefs.putString("key2", anotherStringValue); 

You would just need to convert your string array to and from a string.

nasch
  • 5,330
  • 6
  • 31
  • 52
  • SharedPreferences scale badly. It is ok to store 10 or 20 values, maybe even 100. But they are not intended to be used as a primary persistence tool of an app. Consider using a database if the user data is expected to grow. – vap78 Jan 25 '15 at 20:38
  • It will probably get hard to manage if you're storing a lot of stuff, but what scale problems are you referring to? Do reads and writes become slow if there are a lot of fields? – nasch Jan 26 '15 at 17:53
  • I agree with @vap78. A database is preferred, when dealing with larger amounts of data. `SharedPreference` should be used when the data set is smaller. – Marcus Feb 13 '15 at 20:02
  • @Marcus Because of manageability, or performance, or both? If performance, do you have any specifics of when SharedPreferences becomes a problem? – nasch Feb 13 '15 at 20:04
  • Manageability, of course. This question explains the largest differences between the two. http://stackoverflow.com/questions/6276358/pros-and-cons-of-sqlite-and-shared-preferences – Marcus Feb 13 '15 at 20:08
  • To quote Jodes in the comment section of the accepted answer: "To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through." – Marcus Feb 13 '15 at 20:09
  • From the code we've seen here, it looks like he has two values to save, so I don't know why @Marcus and vap78 think SharedPreferences is going to be unmanageable. A SQL database to store two values is major overkill. – nasch Feb 14 '15 at 21:29
  • @Ethan has two **variables**, `name` and `file_name` which makes for a good table. What happends when @Ethan has let's say 1000 different `file`s to manage? Also, using a database will be more scaleable. @nasch – Marcus Feb 14 '15 at 21:36
  • Sure, but I don't think it's good practice to assume everything is going to be 500 times bigger and more complicated than it starts out. You end up overengineering almost everything because you'll usually be wrong. It's a waste of time and effort, unless there's a good reason to think it will get a lot bigger and more complex. – nasch Feb 14 '15 at 22:43
  • As I'd love to take this discussion further, I think we have to agree to disagree on this matter @nasch. I have a background dealing with databases and I'm clearly biased in this matter, but I do also see your points in this. In the end, I guess this is a matter of personal preferences. – Marcus Feb 14 '15 at 23:19
0

You should consider implementing database support for your application. Database data is easier to maintain, in contrast to SharedPreferences. Here is a good tutorial that explains how to implement an SQLite database.

Marcus
  • 6,697
  • 11
  • 46
  • 89