访问变量在活动另一类 [英] access the variable in activity in another class

查看:125
本文介绍了访问变量在活动另一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序需要从一个活动到另一个活动的变量,而不使用任何意图。因此,我已宣布该变量为静态,并用作 FirstActivity.a 但是这回这么空,所以我创建了扩展应用程序类,并声明该变量还有我我得到空..不知道如何实现这一目标。

Google搜索了很多,但每个人所提出的建议要么使用静态或扩展应用类可惜都没有为我工作。

应用类:

 公共类ApplicationClass扩展应用{    私人字符串StockName;    公共字符串getStockName(){
        返回StockName;
    }    公共无效setStockName(字符串stockName){
        StockName = stockName;
    }}

在一个活动设置变量:

 公共类详细延伸活动{ApplicationClass交流; 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.stockdetail);
        交流=新ApplicationClass();        ac.setStockName(。getIntent()getExtras()的getString(StockName));
}

Retriving变量在另一个类:

 公共类表{    上下文C1;    光标C;
    ApplicationClass交流=新ApplicationClass();公共字符串selectdate =选择+ COLUMN1 +为_id,+列2 +由+表名+去哪儿+栏3 +=
                                + ac.getStockName();

在不知道如何做到这一点..请建议我。

感谢您的时间。

Edit--------------------------------------------------------------------------------------------

 公共类详细延伸活动{公共静态SNAME; 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.stockdetail);        的sname = getIntent()getExtras()的getString(StockName)。。
}公共类表{        上下文C1;        光标C;    公共字符串selectdate =选择+ COLUMN1 +为_id,+列2 +由+表名+去哪儿+栏3 +=
                                    + Detail.sname;


解决方案

既然你说,有来自该行 getIntent()返回的值。getExtras()的getString(StockName),那么试试这个code:

 公共类详细延伸活动{公共静态字符串stringValue的; //使公共的和静态 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.stockdetail);        stringValue的= getIntent()getExtras()的getString(StockName)。。
}

现在访问静态对象表类:

 公共类表{
        上下文C1;
        光标C;      公共字符串selectdate =选择+ COLUMN1 +为_id,+列2 +由+表名+去哪儿+栏3 +=+ Detail.stringValue;}

这应该正常工作。请确保您所访问后,将创建活动的详细 stringValue的变量。

[原创答案]

试试这个:

 公共类详细延伸活动{公共静态ApplicationClass交流; //使公共的和静态 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.stockdetail);
        交流=新ApplicationClass();        ac.setStockName(。getIntent()getExtras()的getString(StockName));
}

现在访问静态对象类:

 公共类表{    上下文C1;    光标C;公共字符串selectdate =选择+ COLUMN1 +为_id,+列2 +由+表名+去哪儿+栏3 +=
                                + Detail.ac.getStockName();
}

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

  Class_Name.Object_Name.Method_Name();

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

In not sure how to achieve this.. please suggest me.

Thanks for your time.

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;

解决方案

[Edited]

Since you're saying that there is a value returned from this line getIntent().getExtras().getString("StockName"), then try this code:

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

Now access the static object in Table class:

   public class Table {
        Context c1;
        Cursor c;

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

}

This should work properly. Make sure you're accessing the stringValue variable after the Detail activity is created.

[Original Answer]

Try this:

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

Now access the static object in Table class:

public class Table {

    Context c1;

    Cursor c;

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

P.S. To access the static object/variable, follow this syntax:

Class_Name.Object_Name.Method_Name();

这篇关于访问变量在活动另一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆