1

I'm new to Android development and I will appreciate any help I can get.

I'm designing an app that at some point needs to ask user for his Friends' names in order to work with those names later on, i.e those names will be used in drop-down lists and will be displayed at a separate View.

My question is: what is the best way to efficiently store those names and then be able to get access to them for reading, editing and deleting? The amount of names will not be big (at most 20 items).

In response to the comment about adding more info:

I need a user to specify list of names (strings) that will be used in 2 different Android Activities: 1) This list of names will be used in a Spinner that is a part of an application form 2) This list of names will be used on a separate Activity designed for Manipulating (Editing and Deleting) of existing items and adding new ones.

I also need that after manipulations (editing, deleting and creating new items) with this list changes took place in Both Activities. This list should be available after user exits the app, so as I understand it should be stored somewhere in Internal Storage.

Domiurg
  • 15
  • 4
  • I strongly recommend you posting more information about your question. Take a look to http://stackoverflow.com/questions/how-to-ask – rpax Mar 06 '14 at 19:51

3 Answers3

2

I hate when people answer a question by just posting the link to the docs, so I won't do that.

I will post the link to the docs AND provide an answer:

DOCS: http://developer.android.com/guide/topics/data/data-storage.html (it is actually a good read, not too long, and good to know what your options are).

It looks to me you need to save an ArrayList or something, and you are saying 20 names would be the maximum amount, so I would say you have 3 viable options, which I present here, ordered in ascending order of simplicity using my humble opinion as a comparator:

1- InternalStorage
2- SharedPreferences
3- Very interesting way I just found while researching one of the options to help you, and I will definatelly use this when I need to save a small array of data...

So the steps I would recomend are: put the names in your favourite collection object (ArrayList, HashSet, etc), then refer to those examples for the methods cited above, respectivelly:

1- https://stackoverflow.com/a/22234324/367342 (YES, this a link to a answer given on this thread, I voted it up, I feel better for cheating now).

2- Save ArrayList to SharedPreferences

3- https://stackoverflow.com/a/5703738/367342 <- this
- Convert your data to a JSONObject
- Convert it to a string
- Save this string using shared preferences
- Read it later as a jsonobject

Example on 3 (untested, sorry):

//Convert the ArrayList to json:
JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));

//Make it into a string
String myLittleJason = json.toString();

//save it to the shared preferences
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("KEY_TO_THE_NAMES_OF_MY_DEAR_FRIENDS", myLittleJason).commit();

//Loading it back from the preferences
String loadedJsonString = PreferenceManager.getDefaultSharedPreferences(context).getString("KEY_TO_THE_NAMES_OF_MY_DEAR_FRIENDS", "I have no friends, this is the default string returned if the key was not found, so, jokes aside, better make this a empty JSON string");

//making it into a JSON again
JSONObject loadedJson = new JSONObject(loadedJsonString);

//Converting the Json back into a ArrayList
ArrayList items = loadedJson.optJSONArray("uniqueArrays");

I loved that JSON approach, if you like it too, upvote the original (too ;) ) https://stackoverflow.com/a/5703738/367342

Community
  • 1
  • 1
paulonogueira
  • 2,684
  • 2
  • 18
  • 31
1

You have many options but I will give you two options:

SharedPreferences
SQLLite

If it's temporary and doesn't require intense data manipulation, I would go with SharedPreferences as it's easier to setup and easy to use and recycle.

Si8
  • 9,141
  • 22
  • 109
  • 221
1

If you are going to store only 20 items, maybe the best way is to write and read a file.

public void writeItems(String fileName) {
  final String ls = System.getProperty("line.separator");
  BufferedWriter writer = null;
  try {
    writer = 
      new BufferedWriter(new OutputStreamWriter(openFileOutput(fileName, 
        Context.MODE_PRIVATE)));
    writer.write("Item 1" + ls);
    writer.write("Item 2" + ls);
  } catch (Exception e) {
      e.printStackTrace();
  } finally {
    if (writer != null) {
    try {
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
  }
}

public void readItems(String fileName) {

  BufferedReader br = null;
  try {
    br = new BufferedReader(new InputStreamReader(openFileInput(fileName)));
    String line;

    while ((line = input.readLine()) != null) {
     //do something
    }
  } catch (Exception e) {
     e.printStackTrace();
  } finally {
  if (input != null) {
    try {
    br.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
  }
} 

openFileInput and openFileOutput reference : http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)

rpax
  • 4,468
  • 7
  • 33
  • 57
  • It's a good way and I was thinking about it, but what about deleting a line? It requires rewriting of a whole file. And what about updating content of let's say Spinner that will use that values? – Domiurg Mar 06 '14 at 19:59
  • You always can use [Random access files](http://docs.oracle.com/javase/tutorial/essential/io/rafs.html) – rpax Mar 06 '14 at 20:00