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

passing values from one class to another android

我正在尝试将值从一个 class 传递给另一个。我不能使用意图,因为 class 扩展了一个自定义库,我不能使用包和东西(如果我是对的)。这是我试过的。

我有 2 个 classes callhelper.java 监控传入和传出 calls.when 触发一个操作,它从数据库填充数据并将其发送到另一个 class它会显示一个弹出窗口。

呼叫助手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 class 中,我有一个全局变量和一个方法 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 是我想要设置文本视图以显示从之前的 class 传递过来的消息的方法。此方法设置 Popup 的布局。通过 StandOutWindow.show() 方法在 CallHelper class 中调用弹出窗口。

所以,问题是,每当它显示弹出窗口时,它都会显示 null。我怎样才能让它显示我的消息而不是 null。应该放入什么额外的代码?请帮助解决这个问题。提前致谢。

创建一个 SharedPrefencesHelper class,让它拥有你想在你的各种 class 中使用的所有全局变量,然后你可以在你的应用程序的任何地方使用这些变量,简单 getter methods.Look 上SharedPreferences,非常好用

由于是运行时间数据,不建议使用sharedpreference。创建名称为 RunTimeData.java 的 class。在这里将所有必需的变量声明为静态的。每当您需要保存数据时,将其保存到这些变量中。使用其他 class 中的数据后,请确保通过将变量设置为空来释放变量。

示例。

public class RunTimeData{
public static String name=null;

}

分配数据时请按如下方式使用。

RuntimeData.name="yyyyy";

使用数据时如下。

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

RunTimeData.name=空;

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