【问题标题】:How to get data from Dialogfragment to fragment with interface如何从Dialogfragment获取数据到带有接口的片段
【发布时间】:2019-02-15 19:12:27
【问题描述】:

我正在尝试通过接口将数据从 dialogfragment 获取到片段中。到目前为止,我首先知道,因为它的片段我需要在 MainActivity 上实现接口,然后从那里将数据发送到我想要的任何片段. 我明白这一点,但我不知道该怎么做。 到目前为止,我已经在 MainActivity 中实现了接口,我正在获取数据,但我不知道如何将其发送到片段。

主要活动

     public class MainActivity extends AppCompatActivity
    implements ChangeProfileImgDialog.OnPhotoReceivedListener{
    //these the methods that i implement
       @Override
public void getImagePath(Uri imagePath) {
    Log.d("imagepath",imagePath.toString());
}

@Override
public void getImageBitmap(Bitmap bitmap) {
    Log.d("imagepath",bitmap.toString());
}

对话片段 - 此对话片段从图库/相机中获取图片

  public class ChangeProfileImgDialog extends DialogFragment {

      public interface OnPhotoReceivedListener{
    public void getImagePath(Uri imagePath);
    public void getImageBitmap(Bitmap bitmap);


}

OnPhotoReceivedListener mOnPhotoReceived;

     @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    /*
    Results when selecting new image from phone memory
     */
    if(requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Uri selectedImageUri = data.getData();
        Log.d(TAG, "onActivityResult: image: " + selectedImageUri);
            //send the uri and fragment to the interface
            mOnPhotoReceived.getImagePath(selectedImageUri);
            getDialog().dismiss();
    }

    else if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Log.d(TAG, "onActivityResult: done taking a photo.");

        Bitmap bitmap;
        bitmap = (Bitmap) data.getExtras().get("data");
        //send the bitmap and fragment to the interface
        mOnPhotoReceived.getImageBitmap(bitmap);
        getDialog().dismiss();
    }
}

@Override
public void onAttach(Context context) {
    try{
        mOnPhotoReceived = (OnPhotoReceivedListener)this.getActivity();
    }catch (ClassCastException e){
        Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage(), e.getCause() );
    }
    super.onAttach(context);
}

片段 - 这个片段应该从对话片段中获取数据。但它应该来自 MainActivity。

    public class MyAccountFragment extends Fragment implements View.OnClickListener,
                                       ChangeProfileImgDialog.OnPhotoReceivedListener{

  @Override
public void getImagePath(Uri imagePath) {
    if( !imagePath.toString().equals("")){
        mSelectedImageUri = imagePath;
        mSelectedImageBitmap = null;
        Log.d(TAG, "getImagePath: got the image uri: " + mSelectedImageUri);
    }
}

@Override
public void getImageBitmap(Bitmap bitmap) {
    if(bitmap != null){
        mSelectedImageUri = null;
        mSelectedImageBitmap = bitmap;
        Log.d(TAG, "getImageBitmap: got the image bitmap: " + mSelectedImageBitmap);
    }
}

我希望你能理解我,我想做的只是将数据从对话片段获取到片段。 因为它的片段和我使用接口我需要在 MainActivity 中实现这个接口,然后一些如何将数据移动到片段。 知道如何将数据移动到片段中吗?

【问题讨论】:

  • 据我了解,你有一个活动,在那个活动中你有一个 dialogfragment 和 myaccountfragment。因此,每当用户选择图片时,都会调用 dialogfragment 并从中调用 onActivityResult。而您现在想要将此数据传递给 myaccountfragment。对吗?
  • 从“MyAccountFragment”调用的 dialogFragment。我从 dialogFragment 获得的数据通过接口传递给“MainActivity”。我想做的是将该数据传递给“MyAccountFragment”
  • 您在哪里调用 MyAccountFragment?
  • 来自“MainActivity” - 我使用“onNavigationItemSelected”
  • 那么你必须使用Bundle发送它

标签: android android-fragments interface


【解决方案1】:

您可以为片段添加检查。 每当您在 MainActivity 的覆盖方法中获得回调时 使用getSupportFragmentManager().findFragmentById(pass the id of the container) 或者可以像这样使用getSupportFragmentManager().findFragmentByTag(pass your fragment tag name):-

Fragment fragment = getSupportFragmentManager().findFragmentById(//pass the id of the container)
if(fragment instanceOf MyAccountFragment){
//after this 
fragment.sendDataToFragment(//Your data);
}

【讨论】:

  • 当我将数据传递给“MyAccountFragment”时如何获取数据?
  • 你将在fragment.sendDataToFragment(//你的数据)中发送数据;方法作为参数,可以在片段中使用。
【解决方案2】:

首先创建一个接口

public interface CallBackInterface{
    public void OnCustomerDetailsCallback(String getDetailsData);
}

然后在 Your MyAccountFragment 中实现这个接口

像这样从 MyAccountFragment 调用 DialogFragment

 android.support.v4.app.FragmentManager fm=getActivity().getSupportFragmentManager();
        dialogFragment dFragment = new dialogFragment ();
        if (dFragment != null) {
            dFragment.onAttachToParentFragment(this);
        }
        dFragment.setArguments(bundle);
        dFragment.show(fm, "Dialog Fragment");

 }

在你的对话框FragmendFragment

   public void onAttachToParentFragment(CallBackInterface callBackInterface) {
        if (callBackInterface != null) {
            try {
                this.callBackInterface= callBackInterface;
            } catch (ClassCastException e) {
                throw new ClassCastException(
                        callBackInterface.toString() + " must implement Callback");
            }
        }

在Dialog Fragment中你想要的地方使用回调函数

callBackInterface.onClickAtOKButton(your data);

【讨论】:

  • 那么有了这段代码,我就不需要在“MainActivity”中使用接口了吗?
  • 是的,您不需要在 MainActivity 中使用接口。您可以从 DialogFragment 获取回调到 Fragment
  • 我试过这个 dFragment.onAttachToParentFragment(this); 行代码。但由于某种原因,我没有“onAttachToParentFragment。它只给了我“onAttach”和“onAttachFragment”
猜你喜欢
  • 2021-08-27
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多