简体   繁体   中英

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.

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.

@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. This method sets the layout for the Popup .The pop up is called in CallHelper class by StandOutWindow.show() method.

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.

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

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.

Example.

public class RunTimeData{
public static String name=null;

}

While assigning data do use as follows.

RuntimeData.name="yyyyy";

While using the data use as follows.

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

RunTimeData.name=null;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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