Abhi Abhi - 2 months ago 23
Android Question

Issue in Button Text change from Dialog Fragment

Here there is minor issue Like I had Recyclerview in dialog fragment.ie name of bank in recyclerview When we select one bank in recyclerview and after dialogfragment dismiss that name should be appear on Button ie when we selected Union Bank from dialog fragment it should appear on button.Issue is when we click on button then its text changes rather then on time of dismiss listener

here is Dialog dismissal code:

mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getContext(), mRecyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
Employee e = bank.get(position);
Toast.makeText(getContext(), e.getBank_id() + "" + e.getBank_name(), Toast.LENGTH_SHORT).show();
getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
Employee e = bank.get(position);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor edit = sp.edit();
edit.putString("bankname", e.getBank_name());
edit.commit();
}
});
c.onItemSelect(e.getBank_name());
onDismiss(getDialog());
}


Here is onclick event where dialog opens and where the value should be printed:

select_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getFragmentManager();
DialogRecyclerview dr = new DialogRecyclerview(AccountManagement_banks.this,callback);
dr.setRetainInstance(true);
dr.show(getSupportFragmentManager(), "Dialog");
SharedPreferences st = PreferenceManager.getDefaultSharedPreferences(AccountManagement_banks.this);
String mode=st.getString("bankname","");
select_button.setText(mode);
Toast.makeText(getApplication(),mode,Toast.LENGTH_SHORT).show();
}
});


Same in:

@Override
public void onItemSelect(String text) {
select_button.setText(text);
}


Here I had created new Interface:

public interface CallBack {
void onItemSelect(String text);}

Answer

just create a callback and implement it on your main class (where you want to display the name) and pass the callback instance to adapter. Now dialog fragment, now when you are selecting any item just call callback function which is overridden in main calss and inside this function just change the text of your button.

public interface CallBack {

void onItemSelect(String text);

}

implement this in your main class like

public class MainActivity extends Activity implements CallBack {
.
.
.
public void onItemSelect(String text){
  button.setText(text);
}
.
.
}

when you are opening your dialogfragment from your main activity just pass MainActivity.this as an argument in the dialog constructor. And in your Dialog class constructor write your code like this

private Callback callback;
public YourDialog(Context context, Callback callback){
this.callback = callback;
}

and when you selecting list item just call

callback.onItemSelect(e.getBank_name());

Hope it will help you out.