Wednesday, February 15, 2012

Store array of String or int or list in SharedPreferences android



 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        JSONArray arr = new JSONArray();
        arr.put("Viren");
        arr.put("Android");
        prefs.edit().putString("key", arr.toString());
        prefs.edit().commit();
        try {
            arr = new JSONArray(prefs.getString("key", "{}"));
            System.out.println(arr.getString(1)); 
        } catch (JSONException e) {
            e.printStackTrace();
        }

4 comments:

  1. hello,
    I'm new for android development. Actually I'm trying to develop a app for kids. It displays 10 fruits name and when someone click any textView I want to display image in another screen. I know how to create classes, but the problem is there's 10 pages.. So I'm trying to get my pictures it to an array... but I don't know how to do it... please help me...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hello Chami, I think one way you could design your App would be to use simple Button Widgets on your MainActivity.java XML layout wherein each button has the name of the "Fruit" that the child can click. You will want to learn also how to create a Button Click listener (which is fairly easy to do and many YouTube Videos show how to do this). This way, your Button, once clicked ....can use something called an Explicit "Intent" call to open another activity (for that Fruit). So your Banana Activity could have a Layout which has an ImageView component (that shows the Image of the fruit from your /res/drawable folder (it does not matter which folder you put the asset into as long as you point to it correctly. on your Banana activity where you have your ImageView, you can also have a TextView widget which has a description for the Fruit (like its color, taste, where it grows best etc..)

    Hope this helps a bit

    Alexander

    ReplyDelete
  4. How to store integer values please?

    ReplyDelete

content -->