问题描述:

From several classes in my app I want to get String global values without using Context.

If I declare this variables in Application class, SharedPreferences or Strings.xml, then I must use Context for get/set it values.

Using Singleton is not a good practice as I understand from this post.

Is there any good way for using global variables without using context?

网友答案:

Create a global class or store the string in a place that "makes sense". For instance if you have the class Shoes you could do this:

public class Shoes {

    public static class BRAND {
        public static final String NIKE = "nike";
        public static final String REBOK = "rebok";
        public static final String ADDIDAS = "addidas";
    }

    private String brand;

    public Shoes() {}

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return this.brand;
    }
}

Now you can do this:

public static void main(String[] args) {

    Shoes myShoes = new Shoes();
    myShoes.setBrand(Shoes.BRAND.NIKE);
}

You will find there are many things like this in Android. It would be even better if you used enums instead. Hope this helps.

UPDATE

If you would like to use setters and getters then there are 2 solutions:

  1. The first you would need an instance of the object and since you only want a single instance a singleton design pattern would be required. Really if you synchronize correctly and the design makes sense they can be very good and useful. In your situation I don't think it would be worth the work.
  2. You can take advantage of the static initializer and static methods. You could just remove the final declaration and do what you want with the Strings, like this:

    public class Shoes {
    
        public static class BRAND {
            public static String NIKE;
            public static String REBOK;
            public static String ADDIDAS;
    
            static {
                NIKE = "nike";
                REBOK = "rebok";
                ADDIDAS = "addidas";
            }
        }
    
        private String brand;
    
        public Shoes() {}
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public String getBrand() {
            return this.brand;
        }
    }
    

Or use good encapsulation practices and do this:

public class Shoes {

    public static class BRAND {
        private static String NIKE;
        private static String REBOK;
        private static String ADDIDAS;

        static {
            NIKE = "nike";
            REBOK = "rebok";
            ADDIDAS = "addidas";
        }

        public static String getNIKE() {
            return NIKE;
        }

        public static void setNIKE(String name) {
            NIKE = name;
        }

        public static String getREBOK() {
            return REBOK;
        }

        public static void setREBOK(String name) {
            REBOK = name;
        }

        public static String getADDIDAS() {
            return ADDIDAS;
        }

        public static void setADDIDAS(String name) {
            ADDIDAS = name;
        }
    }

    private String brand;

    public Shoes() {}

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return this.brand;
    }
}

However I must note: If you are doing this because you cannot get a Context then you are missing something. A context can be obtained from anywhere - objects instantiated by the system are given a context as a parameter. If you have your own custom object you can just pass the ApplicationContext as a parameter or the class using the object itself (this).

网友答案:

Store the values in your strings.xml file. Then call it like this.

 String yourString = Context.getResources().getString(R.string.your_string);
相关阅读:
Top