从活动传递数据到对话框

11

我正在寻找一种将数据从一个活动传递到对话框的方法。我尝试调用showDialog(int);,但是我没有看到任何将数据传递给对话框的方法。 为了显示确认信息,我需要将一个字符串传递给对话框 :)

谢谢

4个回答

13

如果你的目标是 Android 2.2 (API 级别为 8 或更高),你可以使用

 public final boolean showDialog (int id, Bundle args)

并且将参数传递到Bundle中。请参阅文档

如果您希望支持旧版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);
         }             
    }
}

太棒了,听起来不错 :) 唯一的问题是,在您的 doSomething() 函数中,我正在从内联 onClick 事件中调用 showDialog,所以似乎无法通过值传递,我不确定为什么... - jsw

2
我知道这个问题很旧,但如果你正在使用自定义对话框,你可以使用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");

1
当您执行showDialog(int)时,Activity的onCreateDialog方法会被调用。在那里,您必须创建一个对话框实例并返回它,然后它将被显示。
然后,当您创建对话框时,您可以完全访问类的字段,并使用它们的值来调整所创建的对话框的参数和内容。

0
//`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`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接