質問

Hi have one database and I've created a Database class that has a private static class DbHelper extends SQLiteOpenHelper to help me manage my database.

This database is access from four different activities.

Now What I need is? I have a GlobalClass like this:

public class Question extends Application{
     private String check;
     public String getCheck() {
         return check;
     }
     public void setCheck(String check) {
         this.check = check;
     }
}

In FirstScreen Activity I have a value for String check. If I get in other Activity Class its fine, no problem.

If I get in DBHelper I can't. I have tried like this:

final Question quiz = (Question) getApplicationContext();

final String check  = quiz.getCheck();

it shows error in getApplicationContext(). How can I get that value in DBHelper class

Please let me know what is wrong with the syntax.

役に立ちましたか?

解決

in DBHelper you will not have ApplicationContext till you don't pass it.

instead of this do one thing make a static String in your Application class and use it. as Application class is a single ton instance which remains in memory till the end so it will not use much memory.

public class Question extends Application{
    public static String check = "";
}

Retrieval of the value

public class DBHelper {

    public method() {
        String check = Question.check;
    }

}

他のヒント

You don't have a Context in your DBHelper class.

You either need to pass in a Context when you instantiate the DBHelper class (preferred but not always practical) or get the Context from your Question by doing something like the following.

((Question)Question.getAppContext())

The Context is then returned via a getAppContext() method in Question.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top