簡體   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