如何正确扩展和使用应用程序

时间:2023-01-08 20:54:53

In my application i have to share various java-beans class among the activities.

在我的应用程序中,我必须在活动*享各种java-beans类。

In order to do that, i extended the Application class, in which i create an HashMap filled with all the java-beans. Each java-beans has its own Key.

为了做到这一点,我扩展了Application类,在其中我创建了一个填充了所有java-beans的HashMap。每个java-beans都有自己的Key。

Example code:

public class MyApplication extends Application {

    public static final String CLASSROOM_KEY = "Classroom";

    private HashMap<String, Object> myObjects;

    @Override
    public void onCreate() {
        myObjects = new HashMap<String, Object>();
        myObjects.put(CLASSROOM_KEY, new Classroom());
    }

    public HashMap<String, Object> getMyObjects() {
        return myObjects;
    }
}

This is usable in all the activities, and this is ok. BUT, i have two problems:

这在所有活动中都可以使用,这没关系。但是,我有两个问题:

1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.

1)我还需要在非活动类中使用myObjets,比如utils类,但在这些类中,我不能执行“getApplicationContext()”,因为它们不会扩展Activity。

For example, from my main activity i start a service (but it is in a normal class), and the service calls a query that in turn is in another normal class.

例如,在我的主要活动中,我启动一个服务(但它在普通类中),并且该服务调用一个查询,而该查询又在另一个普通类中。

The query needs an object that is in myObjects!

查询需要myObjects中的对象!

I can't make myObjects public static i think.

我认为我不能使myObjects公共静态。

2) In MyApplication i have to create all my java-beans in advance.

2)在MyApplication中,我必须提前创建所有的java-beans。

What if in the future i wanted to create a new classroom object in addition to the already present one? I should create a new key for it, but it is impossible!

如果将来我想创建一个新的教室对象除了已经存在的教室对象之外怎么办?我应该为它创建一个新密钥,但这是不可能的!

Thanks for your help.

谢谢你的帮助。

UDPATE

I change the question:

我改变了问题:

In this class:

在这堂课:

public class ClassroomUtils {

    private static String result = null;
    private static String studentObjectID = null;

    public static void queryClassroom(String UUID, final Classroom classroom) {

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Classroom");
        query.whereEqualTo("BeaconUUID", UUID);
        query.getFirstInBackground(new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {                        
                    try {
                        result = object.getString("Label");
                    } catch(Exception e1){
                        System.out.println("Vuota");
                    }

                    if(result != null) {
                        Log.i("Classroom", "Retrieved " + result );
                        classroom.setClassroom(result);
                        sendNotification(result);
                        addStudentClassroomRelation(object);
                    }
                } else {
                    Log.e("Classroom", "Error: " + e.getMessage());
                }    
            }
        }); 
    }

i want to avoid to pass the classroom to this method (called from another normal class). How can i access to global objects from this class?

我想避免将教室传递给这个方法(从另一个普通类调用)。如何从此类访问全局对象?

2 个解决方案

#1


I can't make myObjects public static i think.

我认为我不能使myObjects公共静态。

Why not? myObjects is effectively global in scope already. There is nothing to be gained, from a memory management standpoint, by having myObjects be a private data member of Application. If you want Classroom to be a Java singleton, do so. You just have to watch your memory management, as you do with your current implementation.

为什么不? myObjects已经在范围内实际上是全局的。从内存管理的角度来看,通过让myObjects成为Application的私有数据成员,没有任何东西可以获得。如果您希望Classroom成为Java单例,请执行此操作。您只需要观察内存管理,就像使用当前的实现一样。

In MyApplication i have to create all my java-beans in advance

在MyApplication中,我必须提前创建所有的java-beans

No, you do not.

你不可以。

What if in the future i wanted to create a new classroom object in addition to the already present one?

如果将来我想创建一个新的教室对象除了已经存在的教室对象之外怎么办?

Then create another one. Perhaps the right singleton is a School, which holds onto a collection of Classroom objects. Again, your primary near-term issue is one of memory management, so you do not run out of memory because you are trying to keep these objects around all of the time.

然后创建另一个。也许正确的单身人士是学校,它拥有一系列课堂对象。同样,您的主要近期问题是内存管理问题,因此您不会耗尽内存,因为您一直试图保留这些对象。

#2


1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.

1)我还需要在非活动类中使用myObjets,比如utils类,但在这些类中,我不能执行“getApplicationContext()”,因为它们不会扩展Activity。

The best way, I think, is to create the MyApplication class as a singleton. There you can retrieve the data from anywhere by calling getInstance and the corresponding getter/setter for your attributes.

我认为最好的方法是将MyApplication类创建为单例。在那里,您可以通过调用getInstance和属性的相应getter / setter从任何地方检索数据。

Short example:

public class MyApplication extends Application {

    private static MyApplication mInstance;

    public MyApplication getInstance(){
        // this means you have only one existing instance of this class.
        if(mInstance == null){
            // set the context to this MyApplication instance
            mInstance = this;
        }
        // return the instance of this class
        return mInstance;
    }

    // here your stuff for MyApplication
    public HashMap<String, Object> getMyObjects() {
        return myObjects;
    }
}

Then you can call it from another class like this:

然后你可以从另一个类中调用它,如下所示:

public class CFoo{

    public CFoo(){
        //retrieve myObjects from MyApplication
        MyApplication.getInstance().getMyObjects();
    }
}

#1


I can't make myObjects public static i think.

我认为我不能使myObjects公共静态。

Why not? myObjects is effectively global in scope already. There is nothing to be gained, from a memory management standpoint, by having myObjects be a private data member of Application. If you want Classroom to be a Java singleton, do so. You just have to watch your memory management, as you do with your current implementation.

为什么不? myObjects已经在范围内实际上是全局的。从内存管理的角度来看,通过让myObjects成为Application的私有数据成员,没有任何东西可以获得。如果您希望Classroom成为Java单例,请执行此操作。您只需要观察内存管理,就像使用当前的实现一样。

In MyApplication i have to create all my java-beans in advance

在MyApplication中,我必须提前创建所有的java-beans

No, you do not.

你不可以。

What if in the future i wanted to create a new classroom object in addition to the already present one?

如果将来我想创建一个新的教室对象除了已经存在的教室对象之外怎么办?

Then create another one. Perhaps the right singleton is a School, which holds onto a collection of Classroom objects. Again, your primary near-term issue is one of memory management, so you do not run out of memory because you are trying to keep these objects around all of the time.

然后创建另一个。也许正确的单身人士是学校,它拥有一系列课堂对象。同样,您的主要近期问题是内存管理问题,因此您不会耗尽内存,因为您一直试图保留这些对象。

#2


1) I need to get myObjets also in non-activity classes, like utils classes, but in these classes i can't do "getApplicationContext()" because they don't extend Activity.

1)我还需要在非活动类中使用myObjets,比如utils类,但在这些类中,我不能执行“getApplicationContext()”,因为它们不会扩展Activity。

The best way, I think, is to create the MyApplication class as a singleton. There you can retrieve the data from anywhere by calling getInstance and the corresponding getter/setter for your attributes.

我认为最好的方法是将MyApplication类创建为单例。在那里,您可以通过调用getInstance和属性的相应getter / setter从任何地方检索数据。

Short example:

public class MyApplication extends Application {

    private static MyApplication mInstance;

    public MyApplication getInstance(){
        // this means you have only one existing instance of this class.
        if(mInstance == null){
            // set the context to this MyApplication instance
            mInstance = this;
        }
        // return the instance of this class
        return mInstance;
    }

    // here your stuff for MyApplication
    public HashMap<String, Object> getMyObjects() {
        return myObjects;
    }
}

Then you can call it from another class like this:

然后你可以从另一个类中调用它,如下所示:

public class CFoo{

    public CFoo(){
        //retrieve myObjects from MyApplication
        MyApplication.getInstance().getMyObjects();
    }
}