user1926837
user1926837

Reputation:

Best way to save arraylist to androids memory

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;

}

}

Upvotes: 1

Views: 5039

Answers (2)

Muhammad Nabeel Arif
Muhammad Nabeel Arif

Reputation: 19310

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

Upvotes: 0

Serdar Samancıoğlu
Serdar Samancıoğlu

Reputation: 740

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);

Upvotes: 4

Related Questions