I need to get the response of the pressed button from a FragmentDialog to an adapter.So basically i've set a click listener for a button in the viewholder and now when i press that button the dialog opens and i need to get the response of the two buttons that i set from that dialog.
My FragmentDialog:
public class FragmentDialog extends AppCompatDialogFragment{
private EditText edtTextAmount;
private TextView txtEnterAmount;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.fragment_add_amount,null);
builder.setView(view)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
edtTextAmount=view.findViewById(R.id.edtTtxt_enterAmount);
return builder.create();
}
}
My Adaptor:
public class WatchlistAdaptor extends RecyclerView.Adapter<WatchlistAdaptor.WatchlistViewHolder> {
//DescriptionFragment descriptionFragment;
private LayoutInflater inflater;
private ArrayList<WatchlistItems> watchlistItems;
private Context context;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onDeleteClickRV(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
class WatchlistViewHolder extends RecyclerView.ViewHolder {
private ImageView cryptoImg;
private TextView cryptoAcronym;
private TextView cryptoName;
private Button btnAddToActCurrencies;
WatchlistViewHolder(View itemView/*, WatchlistAdaptor adaptor*/, final OnItemClickListener listener) {
super(itemView);
this.cryptoImg = itemView.findViewById(R.id.img_wallet_WatchlistRV_cryptoImage);
this.cryptoAcronym = itemView.findViewById(R.id.txt_wallet_WatchlistRV_cryptoAcronym);
this.cryptoName = itemView.findViewById(R.id.txt_wallet_WatchlistRV_cryptoName);
this.btnAddToActCurrencies = itemView.findViewById(R.id.btn_wallet_WatchlistRV_AddToActCurrencies);
// this.animAlpha = AnimationUtils.loadAnimation(context,R.anim.anim_alpha);
btnAddToActCurrencies.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentActivity activity = (FragmentActivity) (context);
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentDialog fragmentDialog = new FragmentDialog();
fragmentDialog.show(fragmentManager, "fragmentalert");
}
}
});
}
}
... }
Example:
public class FragmentDialog extends AppCompatDialogFragment{
private EditText edtTextAmount;
private TextView txtEnterAmount;
// Add listener class and instance
private OnButtonClickListener mListener;
interface OnButtonClickListener() {
void onButtonClick();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
View view=inflater.inflate(R.layout.fragment_add_amount,null);
builder.setView(view)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Invoke listener on UI click
if (mListener != null) {
mListener.onButtonClick();
}
}
});
edtTextAmount=view.findViewById(R.id.edtTtxt_enterAmount);
return builder.create();
}
@Override
public void onAttach(Context context) {
// Save context the fragment is attached to as the listener
if (context instanceof OnButtonClickListener) {
mListener = (OnButtonClickListener) context;
}
}
}
Example:
public void MyActivity extends AppCompatActivity implements FragmentDialog.OnButtonClickListener {
@Override
public void onButtonClick() {
// Since the activity implements the interface, it will be
// called when the fragment button is clicked -
// tell your adapter or whatever what to do
mAdapter.handleButtonClick();
}
}
Hope that helps!