简体   繁体   English

将值从一个类传递到另一个android

[英]passing values from one class to another android

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. 这是我尝试过的。

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. 我有2个类callhelper.java,它监视传入和传出的呼叫。触发动作时,它将填充数据库中的数据并将其发送到另一个显示弹出窗口的类。

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

In popupdisplay class, i have a global variable and a method setData() which sets the value to the variable. 在popupdisplay类中,我有一个全局变量和将值设置为变量的setData()方法。

@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 is the method is where i want to set the textview to display my message which is passed from previous class. createAndAttachView是该方法,我想在其中设置textview以显示从上一类传递的消息。 This method sets the layout for the Popup .The pop up is called in CallHelper class by StandOutWindow.show() method. 此方法设置弹出窗口的布局。弹出窗口由StandOutWindow.show()方法在CallHelper类中调用。

So, the problem is, whenever it displays the popup, it displays null . 因此,问题在于,无论何时显示弹出窗口,都显示null How can i make it display my message instead of null. 我如何使其显示我的消息而不是null。 what extra code should be put in? 应该输入什么额外的代码? Please help regarding this. 请对此提供帮助。 Thanks in advance. 提前致谢。

创建一个SharedPrefencesHelper类,使其具有要在各个类中使用的所有全局变量,然后可以使用简单的getter方法在应用程序中的任何位置使用这些变量。查找SharedPreferences,非常易于使用。

Since this is run time data, it is not suggested to use sharedpreference. 由于这是运行时数据,因此不建议使用sharedpreference。 Create a class with name RunTimeData.java. 创建一个名称为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. 在其他类中使用数据之后,请确保将它们设置为null来释放变量。

Example. 例。

public class RunTimeData{
public static String name=null;

} }

While assigning data do use as follows. 分配数据时,请按照以下说明使用。

RuntimeData.name="yyyyy"; RuntimeData.name =“ yyyyy”;

While using the data use as follows. 使用数据时,请按照以下方式使用。

TextView tv=new TextView(); TextView tv = new TextView(); tv.setText(RunTimeData.name); 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. 当您确定不会在某个地方再次使用该值时,请释放该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM