简体   繁体   中英

How to pass data from alert dialog fragment to recyclerview adapter

我是android的初学者,我如何将数据从警报对话框传递到recyclerview适配器,因为我无法在Internet或stackoverflow中找到主题

An alert dialog is basically just an activity with a change to the AndroidManifest.xml so if you can create an activity and edit the theme in the manifest to something like this:

        <activity
        android:name=".DialogActivity"
        android:label="@string/title_activity_dialog"
        android:theme="@style/Theme.AppCompat.Dialog.Alert" />

You can treat the activity as any normal activity and add whatever you want from it to a recyclerview adapter. It would be helpful if you can share some of what you have attempted to better get an idea however.

You need a callback. An interface will be implemented on the recyclerview host. Then the DialogFragment will use the life cycle to obtained the interface.

Interface Callback {
     void passData(YourObject object);
}
ActivityOrFragment extenda WHATEVER implements Callback {

    @Override
    void passData(YourObject object){adapter.addData(object);}
    //you have to create the method in the adapter and uodate usibg notify data set change or something suitable
}
YourDialogFragment extends DialogFragment {

    private Callback callback;

   //If the recycler is in an activity
   @Override
   onAttach(Context contex){
        callback = (Callback) context;
   }

   //If the recycler is in a fragment
   @Override
    onViewCreated(...){
          //find the fragment and initialize the callback
         callback = getFragmentManager...
    }
}

The user probably interact with the dialog so once thar interqction is done use the callback

callback.passData(userCreatedObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM