-2

I'm using the lib betterpickers, I tried to make a class to get its information in OOP form, but the method does not have a return, it's a void, is there a way to get 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) {
   //This is the method
}
}

I call it in MainActivity like this:

Inflate inflate = new Inflate();

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

I've already tried something like onDateSet calling 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;
}

But how would I get this feedback if I'm calling the piker? That's the problem, is there any way to do it? Because in this activity I have 4 places where a date must be defined, if for each one I have to do a different method the code gets huge.

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

Woton Sampaio
  • 469
  • 6
  • 24
  • 1
    See https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android – ADM Apr 05 '18 at 15:11
  • @ADM I saw and did not understand very well, I am new to java kkkk, if you can make an example with my code I thank :v – Woton Sampaio Apr 05 '18 at 15:17
  • 1
    Use a listener interface . Above link shows how ! You already have a listener in your code `CalendarDatePickerDialogFragment.OnDateSetListener` so you know how it works . – ADM Apr 05 '18 at 15:39
  • @ADM I did it, if you want to take a look, thank you – Woton Sampaio Apr 05 '18 at 17:30

1 Answers1

1

Finally I did, I changed the method, I did 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;                 //After collecting the date I call the method in MainActivity
                        MainActivity.setData(data, textView);
                    }
                });

        cdp.show(supportFragmentManager, FRAG_TAG_DATE_PICKER);
    }
}

I call it this:

Inflate inflate = new Inflate();
tes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //The date sets the value in the TextView, so I step to call the method
               inflate.piker(getSupportFragmentManager(), tes);
            }
        });

In MainActivity, the method receives and assigns the value to the TextView:

public static void setData(String data, TextView textView) {
        textView.setText(data);
}

Thanks @ADM

Woton Sampaio
  • 469
  • 6
  • 24