-1

i using a specific date library(https://github.com/mohamad-amin/PersianMaterialDateTimePicker)

now i want using it in a button in Recycler View.

now my problem is I have to define two options in library codes :

1 - I should refer to the activity.

2 - i need to use getSupport FragmentManager().

but this page has been extends with Recycler View.Adapter and I do not know how to call these two options on this page.

RecyclerViewAdapter page :

                        PersianCalendar persianCalendar = new PersianCalendar();

                    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance( StudentList.this ,
                            persianCalendar.getPersianYear() ,
                            persianCalendar.getPersianMonth() ,
                            persianCalendar.getPersianDay());




                    datePickerDialog.show( getSupportFragmentManager() , "datePickerDialog" );

In these pictures you can easily understand me :

enter image description here

enter image description here

enter image description here

3 Answers3

1

You activity must implements TimePickerDialog.OnTimeSetListener and implement the abstact methode onTimeSet:

    public class StudentList extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {


 @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

    }
}

and inside onClick

  DatePickerDialog datePickerDialog = DatePickerDialog.newInstance((((StudentList)view.getContext()),  persianCalendar.getPersianYear(),
                        persianCalendar.getPersianMonth(),
                        persianCalendar.getPersianDay());

OR :

DatePickerDialog datePickerDialog =DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

            }
        },  persianCalendar.getPersianYear(),
            persianCalendar.getPersianMonth(),
            persianCalendar.getPersianDay());
Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
0

You could try passing the Activity's context when creating an instance of the adapter:

//Code in your Activity
MyAdapter myAdapter = new MyAdapter(this, ...);

Your adapter:

private Context context;

    public MyAdapter(Context context, ...){
    this.context = context;
    //Rest of the constructor...
    }

By doing that, you could just add do this:

datePickerDialog.show(context.getSupportFragmentManager(), "datePickerDialog");
rxabin
  • 106
  • 1
  • 5
  • 1
    I've done the way you said, but the problem has not been resolved @XabinCoffee –  Mar 21 '18 at 15:10
0

You just need an activity context passed in your constructor. Be sure to call new Adapter(this,...) from activities and new Adapter(getActivity(),...) from fragments.

private Context context;

@Override
public void onClick(View v) {
FragmentManager manager = ((Activity) context).getFragmentManager();
FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
}

try this

Android Geek
  • 8,956
  • 2
  • 21
  • 35