繁体   English   中英

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

[英]passing values from one class to another android

我试图将价值从一类传递到另一类。 我不能使用意图,因为该类扩展了自定义库,并且我不能使用包和东西(如果我正确)。 这是我尝试过的。

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

呼叫助手类

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()方法。

@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以显示从上一类传递的消息。 此方法设置弹出窗口的布局。弹出窗口由StandOutWindow.show()方法在CallHelper类中调用。

因此,问题在于,无论何时显示弹出窗口,都显示null 我如何使其显示我的消息而不是null。 应该输入什么额外的代码? 请对此提供帮助。 提前致谢。

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

由于这是运行时数据,因此不建议使用sharedpreference。 创建一个名称为RunTimeData.java的类。 在此将所有必需变量声明为静态。 每当您需要保存数据时,将其保存到这些变量中。 在其他类中使用数据之后,请确保将它们设置为null来释放变量。

例。

public class RunTimeData{
public static String name=null;

}

分配数据时,请按照以下说明使用。

RuntimeData.name =“ yyyyy”;

使用数据时,请按照以下方式使用。

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

RunTimeData.name = null;

当您确定不会在某个地方再次使用该值时,请释放该变量。

暂无
暂无

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

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