Pass Data From Activity To Dialog

后端 未结 4 1221
一整个雨季
一整个雨季 2021-01-17 21:31

I am looking for a way to pass data from an activity onto a dialog box. I am trying to call showDialog(int);, however i don\'t see a way to pass any data to the

相关标签:
4条回答
  • 2021-01-17 21:57

    If you are targeting Android 2.2 (API Level 8 or higher) you can use

     public final boolean showDialog (int id, Bundle args)
    

    And pass your arguments in Bundle. See documentation.

    If you want to support older Android versions, you should save your arguments in Activity class members and then access them from your onPrepareDialog function. Note that onCreateDialog won't fit your needs as it's called only once for dialog creation.

    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 讨论(0)
  • 2021-01-17 22:00

    When you are doing showDialog(int) the Activity's onCreateDialog method is called. There you must create a dialog instance and return it, and it will be shown.

    Then you are creating a dialog, you have a full access to your class' fields and can use values of them to adjust parameters and content of created dialog.

    0 讨论(0)
  • 2021-01-17 22:00

    I know this question is old, but you can use the setArguments method if you are using a custom dialog.

    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");
    
    0 讨论(0)
  • 2021-01-17 22:07
    //`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 讨论(0)
提交回复
热议问题