简体   繁体   中英

What is the proper way to make my List be read and modified throughout my entire Android Java project?

I have the following List:

List<Map<String, String>> items = new ArrayList<Map<String, String>>();

It's populated with data on the load of my app. I need to use it throughout my entire app, on most Activities and Fragments, with data both being read and being modified.

What is the best and simplest way to make this global without too much complexity?

Note: For reasons that are too much to explain on Stack Overflow, this much be used a List and not as a custom Object with properties.

I would make a class to hold your data ( similar to the Singleton method ), and make the variable static.

class NewClass {

    public static List<Map<String, String>> items = new ArrayList<Map<String,String>>();

}

You can add getter/setter/modifier methods to the above class as you please.

Then in your other classes, you can create local variables which you can define as the list from the other class.

class OldClass {
    private List<Map<String, String>> myItems;

    public OldClass() {
        myItems = NewClass.items;
    }
}

other posts are missing the point. of course he can store the list as an instance member and access it with getters and setters, but what object owns the member?

you can use statics as has been suggested, but there's a better way ... just hang it off of your application class. create a class that extends Application , and add the instance there,

public class MyApplication extends Application { 
    private List<Map<String, String>> items = new ArrayList<Map<String, String>>();

    public List<Map<String, String>> getItems() {
        return items;
    }

    public void setItems(List<Map<String, String>> items) {
      this.items = items;
    }
}

configure the application class into your manifest,

<application
    android:name=".MyApplication"
    ...

now, in your activities, or services, just do this,

List<Map<String, String>> items = ((MyApplication)getApplication()).getItems();

from your fragments, do this,

List<Map<String, String>> items = ((MyApplication)getActivity().getApplication()).getItems();

and the nice thing is that this object has the same lifecycle as your application. it exists while any part of your application is active, and gets GC'd along with your application.

Create an abstract helper class and stick your list in there, that way you can access it from anywhere you like.

public abstract class HelperClass() {

    public static List<Map<String, String>> items 
                                = new ArrayList<Map<String,String>>();

    static {
        //populate the list with anything you need when the class is loaded.
    }

}

Making your class abstract will prevent someone from instantiating it. If you need any elements in your list when the application first runs then you can do this in a static initialiser block as shown above.

This will allow you to access it to anywhere in your application.

private List<Map<String, String>> items = new ArrayList<Map<String, String>>();


public List<Map<String, String>> getItems() {
    return items;
}



public void setItems(List<Map<String, String>> items) {
    this.items = items;
}

The preferred way to accomplish this is with getters and setters. Make the list itself private and create a public getList and publi setList methods to get and set the list from anywhere in your app.

public List getList() {
    return items;
}

public void addItems(String item) {
    //code to add items
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM