访问另一个类中的活动中的变量

时间:2014-03-31 10:49:28

标签: android

在我的应用程序中,我需要一个从一个活动到另一个活动的变量,而不使用任何意图。所以我已经将变量声明为静态并用作FirstActivity.a,但这是返回null,因此我创建了一个扩展应用程序的类,并声明该变量仍然是我得到null ..没有线索如何实现这一点

Googled a lot but everyone are suggesting either to use static or extend Application class不幸的是,两人都不适合我。

申请类:

public class ApplicationClass extends Application{

    private String StockName;

    public String getStockName() {
        return StockName;
    }

    public void setStockName(String stockName) {
        StockName = stockName;
    }



}

将一个活动中的变量设置为:

public class Detail extends Activity{

ApplicationClass ac;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockdetail);
        ac=new ApplicationClass();

        ac.setStockName(getIntent().getExtras().getString("StockName"));
}

将另一个类中的变量重新转换为:

public class Table {

    Context c1;

    Cursor c;
    ApplicationClass ac=new ApplicationClass();

public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " 
                                + ac.getStockName();

不确定如何实现这一点..请建议我。

感谢您的时间。

编辑---------------------------------------------- ----------------------------------------------

public class Detail extends Activity{

public static sname;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockdetail);

        sname=getIntent().getExtras().getString("StockName");
}

public class Table {

        Context c1;

        Cursor c;

    public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " 
                                    + Detail.sname;

4 个答案:

答案 0 :(得分:3)

您应该在清单中定义子类应用程序类。你永远不应该打电话给#34;新的ApplicationClass()"。您可以使用activity的getApplication()方法获取对ApplicationClass实例的引用。

Detail.java:

public class Detail extends Activity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.stockdetail);
    ApplicationClass app = (ApplicationClass)getApplication();
    app.setStockName("blah");
}
}

Table.java

public class Table {
public String selectDate;
public Table(Activity a)
{
    ApplicationClass ac=(ApplicationClass)a.getApplication();
    selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " 
                            + ac.getStockName();
}

实例化Table.java

public NewActivity extends Activity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Table t = new Table(this);

}
}

答案 1 :(得分:1)

<强> [编辑]

由于您说的是此行getIntent().getExtras().getString("StockName")返回的值,请尝试以下代码:

public class Detail extends Activity{

public static String stringValue; //make it public and static

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockdetail);

        stringValue = getIntent().getExtras().getString("StockName");
}

现在访问Table类中的静态对象:

   public class Table {
        Context c1;
        Cursor c;

      public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " + Detail.stringValue;

}

这应该正常工作。确保在创建stringValue活动后访问Detail变量。

[原始答案]

试试这个:

public class Detail extends Activity{

public static ApplicationClass ac; //make it public and static

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockdetail);
        ac=new ApplicationClass();

        ac.setStockName(getIntent().getExtras().getString("StockName"));
}

现在访问Table类中的静态对象:

public class Table {

    Context c1;

    Cursor c;

public String selectdate="Select " + column1 + " as _id, " + column2 + " From " + tablename + " Where " + column3 + " = " 
                                + Detail.ac.getStockName();
}

P.S。要访问静态对象/变量,请遵循以下语法:

Class_Name.Object_Name.Method_Name();

答案 2 :(得分:1)

试试这个。

步骤1:在Application类中创建一个静态Bundle对象。(ApplicationClass.java)

例如:

     public static Bundle mMyAppsBundle = new Bundle():

第2步:

从任何地方设置该捆绑中的键值对。 像这样:

   ApplicationClass.mMyAppsBundle.putString("key","value");

第3步:

现在您可以通过以下方式从任何地方获取这些值:

   String str = ApplicationClass.mMyAppsBundle.getString("key");

请在使用捆绑对象之前应用空检查以获得安全观点。

答案 3 :(得分:0)

首先尝试初始化你的课程。但是我看到你想要一些可以通过应用程序访问的应用程序上下文。对于该porpouse,您可以简单地使用该方法,但数据会尝试保留在SharedPreferences中。因此,当您从ApplicationClass获得某些信息时,您只需先从共享首选项中获取并返回。 :)每次当你需要ApplicationClass时,你都会初始化它,并且方法运行共享首选项来获取数据。

public class Detail extends Activity{

ApplicationClass ac = new ApplicationClass();

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stockdetail);
        ac=new ApplicationClass();

        ac.setStockName(getIntent().getExtras().getString("StockName"));
}

共享首选项上下文类。

public ApplicationClassWithSharedPreferences{
   private Context context;
   public ApplicationClassWithSharedPreferences(Context c){
       context = c;
   }

   public String getSomeValueFromContext(){
      SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);
      String highScore = sharedPref.getString("KEY", "DEFAULT");
      return highScore;

   }

}