Pass Data From Activity To Dialog

后端 未结 4 1253
一整个雨季
一整个雨季 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条回答
  •  萌比男神i
    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`
    

提交回复
热议问题