从一个类将值传递给另一个Android [英] passing values from one class to another android

查看:189
本文介绍了从一个类将值传递给另一个Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从一个类传递价值到另一个。因为类扩展自定义库,我不能使用意图,我不能用包和填料(如IM右)。以下是我都试过了。

Im trying to pass value from one class to another. I cannot use intent because the class extends a custom library and i cannot use bundles and stuffs(If im right). Here is what i have tried.

我有2个班callhelper.java它监视传入和传出calls.when的行动被触发时,它从数据库填充数据并将其发送到另一个类的地方显示一个弹出窗口。

I have 2 classes callhelper.java which monitors Incoming and Outgoing calls.when an action is triggered, it populates data from DB and sends it to another class where it displays a popup.

callhelper类

callhelper class

public void onReceive(Context context, Intent intent) {
             String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                String out = null;

            String query = "select * from ReminderTable" ;
            Cursor c2=   enter.selectQuery(query);
             //GetData and place it in out

    //Instance of 2nd class PopUpDisplay set = new PopUpDisplay();
    //passing out to function   set.setData(out);
    // calling 2nd class to display popup //  StandOutWindow.show(context, PopUpDisplay.class, StandOutWindow.DISREGARD_ID);
                     Toast.makeText(ctx, out, Toast.LENGTH_LONG).show();
             enter.close();

在popupdisplay类中,我有一个全局变量和方法使用setData()这将值设置为变量。

In popupdisplay class, i have a global variable and a method setData() which sets the value to the variable.

@Override
public void createAndAttachView(int id, FrameLayout frame) {
        // create a new layout from body.xml
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
     view = inflater.inflate(R.layout.simple, frame, true);
     tv1 = (TextView)view.findViewById(R.id.tvdisplaypoptitle);
        tv = (TextView) view.findViewById(R.id.tvdisplaypopnote);

        tv.setText(note);
        tv1.setText("Title");
    btn1 = (Button) view.findViewById(R.id.btn3);
    btn1.setOnClickListener(this);

}

public void setData(String a){
        note = a;
}

createAndAttachView是方法是我要设置TextView的,以显示我的消息是从previous类传入。该方法通过 StandOutWindow.show设定为弹出.The弹出的布局被称为CallHelper类()方法。

所以,问题是,每当它显示弹出,它会显示。我怎样才能使它显示我的消息,而不是空的。什么额外的code应该放?请帮助对此。先谢谢了。

So, the problem is, whenever it displays the popup, it displays null. How can i make it display my message instead of null. what extra code should be put in? Please help regarding this. Thanks in advance.

推荐答案

由于这是运行时间数据,它不建议使用共享preference。创建名为RunTimeData.java类。这里声明所有必需的变量为static。每当你需要保存的数据,将其保存到这些变量。使用其他类中的数据后,请确保你将它们设置为null RELASE的变量。

Since this is run time data, it is not suggested to use sharedpreference. Create a class with name RunTimeData.java. Declare all required variables here as static. Whenever you need to save data, save it to these variables. After using the data in other class make sure you will relase the variables by setting them to null.

例。

public class RunTimeData{
public static String name=null;

}

虽然分配数据并使用如下:

While assigning data do use as follows.

RuntimeData.name =YYYYY;

RuntimeData.name="yyyyy";

当使用数据使用如下:

TextView的电视=新的TextView();
tv.setText(RunTimeData.name);

TextView tv=new TextView(); tv.setText(RunTimeData.name);

RunTimeData.name = NULL;

RunTimeData.name=null;

释放该变量,当你确信你不会使用值再次的地方。

Release the variable when you are sure that you are not going to using the value once again somewhere.

这篇关于从一个类将值传递给另一个Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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