Ravi Parmar Ravi Parmar - 1 year ago 42
Android Question

How to pass the data in nested Dialog fragment

I have a dialog fragment which has six buttons.when a user clicks on the button, a color picker is open.The color data is then pass to the first dialog fragment and individual color is set on the button.
Dialog Fragment 1 .
The problem is when i declare the interface method,i am able to get the data in fragment or activity but not able to get the data to the dialog fragment 1.
How to pass the data from the dialog fragment 2 to dialog fragment 1 ?

I want to pass the touchedRGB color to first dialog fragment.

Is it possible to send data using getter and setter in the color model,if yes how to do it?

builder.setView(layout)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
ColorDialog.this.getDialog().cancel();
mListener.onDialogNegativeClick(ColorDialog.this);//calling the listener

}
})
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
ColorModel colormodel = new ColorModel();
colormodel.setColorData(ColorData);
mListener.onDialogPositiveClick(ColorDialog.this);
mListener.receiveColorData(touchedRGB); //calling the listener
}
});
return builder.create();
}


Dialog Fragment 1

public class addMain extends DialogFragment implements View.OnClickListener,ColorDialog.ColorDialogListener {

Button color1,color2,color3,color4,color5,color6,canceladd,okadd;
SeekBar seek = null;
public int touchedRGB = 000000;
Integer[] coloranimchange={0,0,0,0,0,0},customvisibility={0,0,0,0,0,0};
Button scenes;
EditText custtomname;
String theme;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.ColoePickerDialogStyle);
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.dialogadd, null);


color1 = (Button) layout.findViewById(R.id.addcolor1);
color2 = (Button) layout.findViewById(R.id.addcolor2);
color3 = (Button) layout.findViewById(R.id.addcolor3);
color4 = (Button) layout.findViewById(R.id.addcolor4);
color5 = (Button) layout.findViewById(R.id.addcolor5);
color6 = (Button) layout.findViewById(R.id.addcolor6);
custtomname = (EditText) layout.findViewById(R.id.themename);

color1.setOnClickListener(this);
color2.setOnClickListener(this);
color3.setOnClickListener(this);
color4.setOnClickListener(this);
color5.setOnClickListener(this);
color6.setOnClickListener(this);

builder.setView(layout)
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
addMain.this.getDialog().cancel();

}
})
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {

}
});
return builder.create();

}


public static String[] getArray(String input) {
return input.split("\\|\\$\\|SEPARATOR\\|\\$\\|");
}

@Override
public void onClick(View view) {
ColorDialog dialogFragment = new ColorDialog();
dialogFragment.show(getChildFragmentManager(), "ColorSelect");
}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {

}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {

}

@Override
public void receiveColorData(int ColorData) {
String col = String.valueOf(ColorData);
Log.v("col",col);
}
}


Thank you in advance.

Answer Source

You were just implement the ColorDialog.ColorDialogListener, but you forgot to set callback to ColorDialog.

Dialog Fragment 1

ColorDialog dialogFragment = new ColorDialog();
dialogFragment.setCallBack(this);
dialogFragment.show(getChildFragmentManager(), "ColorSelect");

ColorDialog

public ColorDialog extends DialogFragment {
   private ColorDialogListener mCallback;

   public void setCallBack(ColorDialogListener listener) {
       this.mCallback = listener;
   }

   public interface ColorDialogListener {
          public void onColorSelect(ColorModel model);
   }

  /** Now you call back is ready to send data**/
  mCallback.onColorSelect(colorModel )
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download