2011-05-25 69 views
9

我正在尋找一種將數據從活動傳遞到對話框的方法。我正在嘗試撥打showDialog(int);,但是我沒有看到將數據傳遞給對話框的方法。 我需要一個字符串傳遞到對話框,以顯示確認:)將數據從活動傳遞到對話框

乾杯

回答

13

如果你的目標的Android 2.2(API級別8或更高版本),你可以使用

public final boolean showDialog (int id, Bundle args) 

並通過您的論點Bundle。請參閱documentation

如果您想支持較舊的Android版本,您應該將您的參數保存在Activity類成員中,然後從您的onPrepareDialog函數中訪問它們。請注意,onCreateDialog不適合您的需求,因爲它僅在創建對話框時被調用一次。

class MyActivity { 

    private static int MY_DLG = 1; 
    private String m_dlgMsg; 

    private showMyDialog(String msg){ 
     m_dlgMsg = msg; 
     showDialog(MY_DLG); 
    } 

    private doSomething() { 
     ... 
     showMyDlg("some text"); 
    } 

    protected void onCreateDialog(int id){ 
     if(id == MY_DLG){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      .... 
      return builder.create(); 
     } 
     return super.onCreateDialog(id); 
    }   

    @Override 
    protected void onPrepareDialog (int id, Dialog dialog){ 
     if(id == MY_DLG){ 
      AlertDialog adlg = (AlertDialog)dialog; 
      adlg.setMessage(m_dlgMsg); 
     } else { 
      super.onPrepareDialog(id, dialog); 
     }    
    } 
} 
+0

真棒,聽起來不錯:)唯一的問題是,你有DoSomething的(),我從一個內嵌onClick事件中調用的ShowDialog,所以它似乎不通過被傳遞的價​​值觀,我m不知道爲什麼... – jsw 2011-05-25 13:31:58

+0

'showDialog(int id,Bundle args)'在API 13中不推薦使用 – 2013-09-11 08:27:52

1

當你正在做的ShowDialog(INT)活動的onCreateDialog方法被調用。您必須創建一個對話框實例並將其返回,並顯示出來。

然後,您將創建一個對話框,您可以完全訪問班級的字段,並可以使用它們的值來調整所創建對話框的參數和內容。

1
//`enter code here`I used shared preferences and it worked. 
**in your activity:** 

//your field: public static final String GAME_PREFERENCES = null; 

         String template = selectedItem.getProduct().getName(); 
      String num = selectedItem.getNumber(); 
      String id = selectedItem.getId(); 
      String location = selectedItem.getLocationName(); 


     SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, 
        MODE_PRIVATE); 
      SharedPreferences.Editor prefEditor = settings.edit(); 
      prefEditor.putString("template", template); 
      prefEditor.putString("num", num); 
      prefEditor.putString("id", id); 
      prefEditor.putString("location", location); 
      prefEditor.commit(); 

**in your dialog class:** 
//In my case I needed to add activity because how i created dialog: 

public MyDialog(Context context, int theme) { 
     super(context, theme); 
     init(context); 
    } 

    private void init(Context context) { 
     setContentView(R.layout.dialog); 
     this.context = context; 
     final Activity activity = (Activity) context; 

       // If you dont call activity you can try context.getpreferences(......) 
     SharedPreferences prefs = ((Activity) context) 
       .getPreferences(Context.MODE_PRIVATE); 

     String template = prefs.getString("template", ""); 
     String num = prefs.getString("num", ""); 
     String id = prefs.getString("id", ""); 
     String location = prefs.getString("location", ""); 

}`enter code here` 
0

我知道這個問題是舊的,但你可以,如果你使用的是自定義對話框使用setArguments方法。

String myString = "How to do it" 
DialogFragment newFragment = new AddUserDialog(); 
       Bundle args = new Bundle(); 
       args.putString("number", myString); //The first parameter is the key that will be used to retrieve the value, which is the second parameter. 
       newFragment.setArguments(args); 
       newFragment.show(getSupportFragmentManager(), "add_a_member"); 
相關問題