Alex Mensak
Alex Mensak

Reputation: 168

Saving data in ArrayList of objects

Im trying to figure out how to save some data in ArrayList of objects, but I'm new in Java so I have had some trouble...

So lets say we have an ArrayList of this object:

public class AppListModel(){
   private String AppName;
   private String packageName;

   public AppListModel(){

   }

   public String getAppName() {
       return appName;
   }

   public String getPackageName() {
       return packageName;
   }

   public void setAppName(String appName) {
       this.appName = appName;
   }

   public void setPackageName(String packageName) {
       this.packageName = packageName;
   }
}

and we have arrayList of this object in difrent file:

public class ProfilesList {

private ArrayList<AppListModel> profilesList = new ArrayList<AppListModel>();

public ProfilesList(){

}

public ArrayList<AppListModel> getProfilesList() {
    return profilesList;
}

public void setProfilesList(ArrayList<AppListModel> profilesList) {
    this.profilesList = profilesList;
}

public void addProfilesList(AppListModel appListModel) {
    this.profilesList.add(appListModel);
}

}

Is it possible to store data in one file like:

AppListModel appList = new AppListModel();
appList.setAppName("ssss");
appList.setPackageName("ddddd");
ProfilesList list = new ProfilesList();
list.addProfilesList(appList);

and then access those data from another file like:

ArrayList<AppListModel> list = new ArrayList<AppListModel>();
ProfilesList profList = new ProfilesList();
list = profList.getProfilesList();

Does the ArrayList named list from last code sample now contain those previously created data?

If not, how can be something like that achieved? do I need use soma databases or something?

I want to use it to process ArrayList between different activities in android.

Thankyou!

Upvotes: 0

Views: 122

Answers (5)

meritus
meritus

Reputation: 68

The list you make here:

AppListModel appList = new AppListModel();
appList.setAppName("ssss");
appList.setPackageName("ddddd");
ProfilesList list = new ProfilesList();
list.addProfilesList(appList);

Won't be the same as the list here:

ArrayList<AppListModel> list = new ArrayList<AppListModel>();
ProfilesList profList = new ProfilesList();
list = profList.getProfilesList();

Anytime you make a new ProfilesList() it is not the same as any other.

public void anyMethod() {
    //list1 is not the same as list2
    ArrayList<AppListModel> list1 = new ArrayList<AppListModel>();
    ArrayList<AppListModel> list2 = new ArrayList<AppListModel>();

    //list3 will be same as list1
    ArrayList<AppListModel> list3 = list1;

    //adding an AppListModel to list1
    AppListModel appList = new AppListModel();    
    list1.add(appList);

    list1.getProfilesList().isEmpty(); //false because it has appList
    list2.getProfilesList().isEmpty(); //true
    list3.getProfilesList().isEmpty(); //false because it refers to list1 which has appList
}

The above shows the difference between the ArrayLists.

Upvotes: 1

pez
pez

Reputation: 4235

If you want to access data from different parts of your app, I would suggest you use SharedPreferences. More information on this can be found here. SharedPreferences are more useful for key-value pairs, however.

In your case, an SQLite database would be more useful. See here. You would create an SQLite table that contains columns for each of your object's fields. For example, a table with columns named AppName, PackageName, etc.

You could simply pass the ArrayList to different parts of your app as an argument, but if you begin dealing with multiple lists, this can be cumbersome and ineffective. The SQLite database will be much more efficient as your app grows.

Upvotes: 1

Ed Holloway-George
Ed Holloway-George

Reputation: 5149

Does the ArrayList named list from last code sample contain those previously created data?

If you use this code:

ProfilesList profList = new ProfilesList();
ArrayList<AppListModel> list = profList.getProfilesList()

You will not get any previously saved data as you have created a new instance of a ProfilesList object. You'd need to use the same instance to get the data back

ProfilesList profList = new ProfilesList();
profList.addProfilesList(...);

//...    

//This would then return the correct data
ArrayList<AppListModel> list = profList.getProfilesList();

I would suggest using a SQLite database if you require more persistent storage

Upvotes: 0

Henrik Wassdahl
Henrik Wassdahl

Reputation: 1140

"new" will always create a new instance of the wanted object.

You want to look up SQLite. It's how you store data in android. http://developer.android.com/training/basics/data-storage/databases.html

Once it's saved on your local phone's database you can retrieve data whenever you want and add it to wanted array lists. However, any changes to the data needs to be committed to the database again for it to be stored permanently.

Upvotes: 0

Diego Martinoia
Diego Martinoia

Reputation: 4662

The "new" keyword creates a new instance of the object (in this case, being a collection, an empty one).

If you want to access the same instance you created before, you need to "pass" it to the point where it's needed. Say that your usage code is wrapped in a function:

void doSomething(ProfilesList profList) {
   ArrayList<AppListModel> list = new ArrayList<AppListModel>();
   list = profList.getProfilesList();
   //do something with list...
}

Then you can call this code by doing something like:

AppListModel appList = new AppListModel();
appList.setAppName("ssss");
appList.setPackageName("ddddd");
ProfilesList list = new ProfilesList();
list.addProfilesList(appList);
doSomething(list);

Upvotes: 1

Related Questions