2

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);}
Ramesh Bhati
  • 1,239
  • 16
  • 25
Abhi
  • 385
  • 1
  • 4
  • 13
  • does your listview inside your fragemnt or it is in same Activity where your button ? – sushildlh Sep 12 '16 at 05:28
  • Pass any handler or custom listener interface when initializing DialogRecyclerview. – Pradeep Kumar Sep 12 '16 at 05:40
  • can you show me demo @pradeep sir – Abhi Sep 12 '16 at 05:45
  • @dipalishah but clicklistener is in activity and ondismisslistener is on dialogfragment I want to settext on activity from dialogfragment – Abhi Sep 12 '16 at 05:47
  • @sushildlh when select button is clicked the dialogfragment opens up and in that there is listview of bank and when we select bank the (select button) should be replaced by (selected bank) – Abhi Sep 12 '16 at 05:48
  • @Abhi Use **OnActivityResult** to set the Text of button. – Dipali Shah Sep 12 '16 at 05:53
  • Can you eloborate little more.StartActivityForResult on dialogrecyclerview and onactivityresult on Activity?? – Abhi Sep 12 '16 at 05:57
  • @Abhi Check this - http://stackoverflow.com/questions/15970196/how-to-execute-action-after-dialogfragment-positive-button-clicked – Dipali Shah Sep 12 '16 at 05:58
  • Still not coming @dipalishah there is no change in button either.. – Abhi Sep 12 '16 at 06:13

1 Answers1

0

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.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30