Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

android - access the variable in activity in another class

In my application I need a variable from one activity to another activity without using any intent. So I have declared that variable as static and used as FirstActivity.a but this is returning so null, Hence I have created a class that extends application and declared that variable there still I am getting null. no clue how to achieve this.

Googled a lot but everyone are suggesting either to use static or extend Application class, unfortunately both are not working for me.

Application class:

public class ApplicationClass extends Application{

    private String StockName;

    public String getStockName() {
        return StockName;
    }

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



}

Setting the variable in one activity as:

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"));
}

Retriving the variable in another class as:

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();

I'm not sure how to achieve this.

Edit

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should define your subclassed application class in your manifest. And you should never call "new ApplicationClass()". You can get a reference to ApplicationClass instance using activity's getApplication() method.

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();
}

Instantiate Table.java

public NewActivity extends Activity{

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

}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...