Pablito Pablito - 1 month ago 7
Android Question

Create a list of object usable in more activity

I want to create a list of object that is accessible by more activity.
I thought to create a class with the list of object (not array). I used this...

public class List{
Object object1 = new Object("this", 59, true)
...
}


And activity

public class ListActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
...
ArrayList<Object> array = new ArrayList<>();
array.add(Object(List.object1));
...}}


in List.object1 there's the error method call expected.

There's a easier method to storage the objects and more efficient?

Answer

I will base this solution that you want to create a list of objects and use it in different activity class.

First solution - You can create a custom application class and instantiate your list of object in the onCreate callback method. This is give you access to your list of object in other class

public class CustomApplication extends Application {

private List<YourClassName> listOfObjects;

@Override
public void onCreate() {
    super.onCreate();

    listOfObjects = new ArrayList<YourClassName>();
}

private List<YourClassName> getListOfObject(){
    return listOfObjects;
}
}

Then to access it, you can use this line of code

List<YourClassName> objectList = ((CustomApplication)getApplication()).getListOfObject();

Second solution - You can use a Wrapper class to wrap your list of objects like below.

Assuming you have this User class

public class User {
private String name;
private String email;
private String username;


public User(String name, String email, String username) {
    this.name = name;
    this.email = email;
    this.username = username;
}

public String getName() {
    return name;
}

public String getEmail() {
    return email;
}

public String getUsername() {
    return username;
}
}

then the Wrapper class will look like this

public class Wrapper {
private List<User> users;

public Wrapper(List<User> users){
    this.users = users;
}

public List<User> getUsers(){
    return  users;
}
}

You can convert your Wrapper class to a Json String

Gson gson = new Gson();
String jsonString = gson.toJson(Wrapper);

You can store you jsonString in a sharepreference like this

SharedPreferences prefs = getSharedPreferences("IDENTIFIER", Context.MODE_PRIVATE);
SharedPreferences.Editor edits = prefs.edit();
edits.putString("LIST_OBJECT", jsonString);
edits.apply();

To get back the list of User in a class, you will use the code below. Get the json string in sharedpreference like this

String jListObject = prefs.getString("LIST_OBJECT", "")

then get list of User objects

Wrapper listObjectWrapper = gson.fromJson(stringObject, Wrapper.class);
List<User> users = listObjectWrapper.getUsers();

Third solution - you can use the TypeToken to convert json string to list of object like the example below

Create a list of User objects

List<User> objList = new ArrayList<User>();
objList.add(new User("high", "zero.yahoo.com", "zero"));
objList.add(new User("low", "one.yahoo.com", "one"));

convert the list object to string

String json = new Gson().toJson(objList);

Store the json string in a sharedpreference using the example in second solution

When you retrieve the json string, you can reconvert it to object list of User as shown below

Type type = new TypeToken<List<DataObject>>(){}.getType();
List<DataObject> inpList = new Gson().fromJson(jsonString, type);

Remember to add this line in your app build.gradle file

compile 'com.google.code.gson:gson:2.4'