TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Sharing an ArrayList Between Activities

I am having problems storing a List in an Application class across activities.

In my Splash screen, I am loading data into a List from a MySQL database.

The List is in a class called Rateit that extends Application. I do this in it as a class variable:

public static List<String> masterCats;

In the Splash Screen Activity/Class, I do this:

in onCreate:

Rateit.masterCats = new ArrayList<String>(); 

Inside my AsyncTask loop where loading data I do this:

Rateit.masterCats.add(cat);

cat being the list item that comes from the database. I have Log.d the data (cat) as well as the ListPosition to check if it is being added to the List and it is.

However, I need to grab that same info in the next Activity and put it into an adapter. It comes back with 0 length.

I simply do this: adapter = new MasterCatAdapter(getActivity(), Rateit.masterCats, tf);

How come the list doesn't maintain data across activies? Is this because its static? something else?

(Note: I will add getter and setter methods here soon!)

Upvotes: 0

Views: 572

Answers (2)

dennisdrew
dennisdrew

Reputation: 4399

As @A--C mentioned in his comment, avoid the model you have now with the static variable. Instead, I would do something like this:

private void yourMethodWhereYouGetYourData(){
    //get your data
    ArrayList<String> masterCats = new ArrayList<String>();
    masterCats.add(cat);

   //Assuming you're doing this synchronously, once you've gotten your data just do:
   Intent i = new Intent(this, YourActivity.class);
   i.putStringArrayListExtra("MasterCats", masterCats);
   startActivity(i);
}

Then, in your new Activity's onCreate() or wherever, just access the list by doing:

getIntent().getStringArrayListExtra("MasterCats");

@KickingLettuce also mentioned in the comments about keeping this accessible if the user navigates away from the Activity. So, in whichever Activity you want to save the ArrayList, just convert it to a comma-separated String and save it in SharedPreferences like so:

private void saveCats(){
    //get your ArrayList from wherever (either as a global variable or pass it into the
    //method.
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < masterCats.size(); i++){
        if(i == masterCats.size() - 1)
            sb.append(masterCats.get(i));
        else
            sb.append(masterCats.get(i)+",");
    }

    SharedPreferences.Editor prefsEditor =
        PreferenceManager.getDefaultSharedPreferences(this).edit();
    prefsEditor.putString("MasterCats", sb.toString()).commit();

    //Note: in API 11 and beyond you can store a Set of Strings in SharedPreferences, so
    //if you are only targeting API 11+ you could do:
    Set<String> masterCatsSet = new LinkedHashSet<String>(); //<--using LinkedHashSet to preserve order
    masterCatsSet.addAll(masterCats);
    prefsEditor.putStringSet("MasterCats",masterCatsSet).commit();
}

Then access this SharedPreference in your onCreate or something if you wish to persist the list across the Activity lifecycle.

Upvotes: 2

saving the List in a Application extended class is good idea you can achieve it by following

Declare List as follows in your App class like

public static List<String> masterCats;

and declare setter and getter methods for above variable

public List getMasterCatsList()
 {
    return masteCats;
 }

public Void setMasterCatsList(List list)
 {
    masteCats=list;
 }

get the application object as follows in your Loader class as follows

Application application = (YOurClassName That Extends Application class) getApplication();

and set the list as follows

  application.setMasterCatsList(List list); 

now you can access this list from any activity as follows

  Application application = (YOurClassName That Extends Application class) getApplication();

 List l = application.getMasterCatsList();

hope it will be helpful to you

Upvotes: 1

Related Questions