1

I have a bundle passed from one activity to another. That contains String n (the length is max 30), String ID and String color. I need to save these values to an ArrayList as an array (n, ID, color) and then to save ArrayList to androids memory. I was looking for a best way of doing that.. I've tried database but its to complicated for me at the moment and I don't think I need such a complex thing. I've tried FileOutputStream (as it explained here: http://developer.android.com/guide/topics/data/data-storage.html#pref?) but it's not working for me, probably because I'm doing something wrong. Do I actually need to create an arraylist of arrays or may be i could use arraylist of bundles, or any other way..? Whats the best way...? Please help..

Thanks every one...Was trying all this time but no luck.. I'm posting the code hoping that someone could give me a hand on that:

public class MainActivity extends Activity
{
String gotNotes;
String n;
String gotDOW;
String gotID;
public String clrs;
public String id;
public String nts;
String gotHour;
String gotColor;
TextView notes;
public static String FILENAME = "allevents";
String[] newevent;
String[] events;
SharedPreferences sharedPref;


  @Override
  public void onCreate(Bundle savedInstanceState) 
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Button settings = (Button)findViewById(R.id.settings);

    Bundle gotPackage = getIntent().getExtras();
    if (gotPackage != null){
    gotNotes = gotPackage.getString("AddedNote");
    if (gotNotes.equals(" "))
      {
        n = "Empty";
      }
    else 
        {
        n = gotNotes;
        }
    //gotDOW = gotPackage.getString("Day");
    //gotHour = gotPackage.getInt("Hour");
    gotID = gotPackage.getString("ID");
    gotColor = gotPackage.getString("color");

    initialize();

    }
    else{}



 settings.setOnClickListener(new OnClickListener()
    {
       public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

    });

 }

private void initialize() 
 {
    // TODO Auto-generated method stub
String[] newevent = {n, gotID, gotColor};

ArrayList<String[]> events = new ArrayList<String[]>();
events.add(newevent);

SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor =     this.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("yourKey", events.toString());
editor.commit();

String allData = sharedPref.getString("yourKey", null);
String[] playlists = allData.split(",");

  /* for (int number=0;number<events.lastIndexOf(sharedPref);number++)
   {

           notes = (TextView)findViewById(getResources().getIdentifier(playlists[number], getString(0), allData));
           notes.setText(number+1);


   }*/



    notes = (TextView)findViewById(getResources().getIdentifier(gotID,     "id",getPackageName()));
    notes.setText(n);
    notes.setGravity(Gravity.CENTER_HORIZONTAL);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    if (gotColor.equals("Blue")){
    notes.setBackgroundColor(Color.rgb(99, 184, 255));}else
    if(gotColor.equals("Green")){
    notes.setBackgroundColor(Color.rgb(189, 252, 201));}else
    if(gotColor.equals("Yellow")){
    notes.setBackgroundColor(Color.rgb(238, 233, 191));}else
    if(gotColor.equals("Grey")){
    notes.setBackgroundColor(Color.LTGRAY);}else    
    if(gotColor.equals("Aqua")){
    notes.setBackgroundColor(Color.rgb(151, 255, 255));}else
    if(gotColor.equals("White")){}

}



    @Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;

}

}
  • Since you're saying that max length of string would be around 30, then you should just save it in `SharedPreferences` like Serdar has already explained. Database would be complicated and uneccessary for your use case. – Sudarshan Bhat Jan 10 '13 at 14:08

2 Answers2

4

Simply use SharedPreferences to save your application's data.

SharedPreferences sharedPref = activity.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = activity.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("yourKey", yourArray.toString());
editor.commit();

To get your array as String do the following:

String arrayString = sharedPref.getString("yourKey", null);
  • Thanks Serdar S. ... What is activity in activity.getPreferences? Sorry I'm a noob.. –  Jan 10 '13 at 14:20
  • It is just your activity. You can use YourActivityName.this instead of it. – Serdar Samancıoğlu Jan 10 '13 at 14:30
  • Ok got that fixed by using this.getPreferences. Could you please explain what is arrayString, "yourKey" and how this conversion works? Thanks! –  Jan 10 '13 at 15:05
  • 1
    I mean how to retrieve those three values from the arrayString –  Jan 10 '13 at 15:11
  • You can save your ArrayList only as "String" type in SharedPreferences. So, arrayString is the string value which you put to SharedPreferences by putString method before. You can check it out on android developer's site: http://developer.android.com/reference/android/content/SharedPreferences.html – Serdar Samancıoğlu Jan 10 '13 at 15:17
0

You can save array into shared preferences and retrieve it back. Here is a good example with fully functional code.

Community
  • 1
  • 1
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70