How to take the return of this method

Asked

Viewed 57 times

1

I’m using the lib betterpickers, I tried to make a class to take your information form OO, however the method does not have a return, it is a void, there is a way to catch this return?

public class Inflate implements CalendarDatePickerDialogFragment.OnDateSetListener{

private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

public void piker(FragmentManager supportFragmentManager){
    CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
            .setOnDateSetListener(this);
    cdp.show(supportFragmentManager, FRAG_TAG_DATE_PICKER);
}

@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
   //Esse é o metodo
}
}

I call you on Mainactivity so:

   Inflate inflate = new Inflate();

   tes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           inflate.piker(getSupportFragmentManager());
        }
    });

I’ve tried something like onDateSet call another method:

@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
    getData(year + "/" + monthOfYear + "/" + dayOfMonth);
}

public String getData(String data){
    return data;
}

However, how would I get that feedback if I’m calling Piker? That’s the problem, is there any way to do it? Because in Activity I have 4 places where a date should be set, if for each one I have to make a different method the code gets huge.

Documentation: https://github.com/code-troopers/android-betterpickers

  • Post the documentation for us to see.

  • @RBZ ready, added

1 answer

1


Finally got it, I changed the method and the class, it was like this:

public class Inflate{

    private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

    public void piker(FragmentManager supportFragmentManager, final TextView textView){
        CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment();
                cdp.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                    @Override
                    public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                        String data = year + "/" + monthOfYear + "/" + dayOfMonth;                 //Após setar a data eu chamo o método na MainActivity para passar a data para o TextView
                        MainActivity.setData(data, textView);
                    }
                });

        cdp.show(supportFragmentManager, FRAG_TAG_DATE_PICKER);
    }
}

I call him that now:

Inflate inflate = new Inflate();
tes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //Agora além do FragmentManager, também passo o TextView que irá receber o valor.

               inflate.piker(getSupportFragmentManager(), tes);
            }
 });

In Mainactivity, this is the method called after setting the date, it receives the date in String and the Textview that will receive the value:

public static void setData(String data, TextView textView) {
       //Então é só setar a data
        textView.setText(data);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.