0

So I have an app where I need to select a date for a filter and the selecting works fine but I can't seem to change a value on the selection screen. This is where I call the SelectDateFragment. There is more code but that isn't important for this.

public class filter extends BottomSheetDialogFragment {

public static filter getInstance() {
    return new filter();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.filter_popup, container, false);
    final TextView dateTextView = (TextView) view.findViewById(R.id.KuupaevValikView);
    final View button1 = view.findViewById(R.id.Nooleke1);
    final View button2 = view.findViewById(R.id.KuupaevValikView);
    final View button3 = view.findViewById(R.id.Nooleke3);
    final View button4 = view.findViewById(R.id.Nooleke4);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating the instance of PopupMenu
            PopupMenu popup1 = new PopupMenu(getActivity(), button1);
            //Inflating the Popup using xml file
            popup1.getMenuInflater()
                    .inflate(R.menu.filtermenuprice, popup1.getMenu());

            //registering popup1 with OnMenuItemClickListener
            popup1.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    ((TextView)view.findViewById(R.id.HinnavahemikValikView)).setText (item.getTitle());
                    Toast.makeText(
                            getActivity(),
                            "You Clicked : " + item.getTitle(),
                            Toast.LENGTH_SHORT
                    ).show();
                    return true;
                }
            });
            popup1.show();
        }}
        );

    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");

        }
    });

And this is what it is calling :

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       final Calendar calendar = Calendar.getInstance();
       int yy = calendar.get(Calendar.YEAR);
       int mm = calendar.get(Calendar.MONTH);
       int dd = calendar.get(Calendar.DAY_OF_MONTH);
       return new DatePickerDialog(getActivity(), this, yy, mm, dd);
   }

   public void onDateSet(DatePicker view, int yy, int mm, int dd) {
       populateSetDate(yy, mm+1, dd);
   }
   public void populateSetDate(int year, int month, int day) {
       String date = month+"/"+day+"/"+year;
       Toast.makeText(getActivity(), date, Toast.LENGTH_SHORT).show();
       //.setText(month+"/"+day+"/"+year);
   }

}

I would like to replace button2 with the string called date, but I am unable to do that.

1 Answers1

0

This should work. If not, let me know and we'll dig deeper into it:

public void populateSetDate(int year, int month, int day) {
   String date = month+"/"+day+"/"+year;
   Toast.makeText(getActivity(), date, ).show();
   button2.text = date;

}

As long as your onDateSet function is working correctly, and you're getting your Toast in populateSetDate, you should be able to use the date String to populate your textView (and you should change the variable name of that later).

BBurchfield
  • 556
  • 4
  • 17
  • Thank you, will try this out and let you know. – Simon Fox Kuuse Oct 09 '19 at 15:17
  • Please do! I believe this should be all you need; but there may be some underlying issue if this doesn't work. – BBurchfield Oct 09 '19 at 15:19
  • This does not work as it does not recognize the variable button2 inside the DialogFragment. – Simon Fox Kuuse Oct 09 '19 at 18:31
  • That's a simple scope issue. This is the correct answer. Just import button2 (which, again, should be renamed) into your function. It seems there are easier ways of pulling this off than nesting fragments; but that's just an opinion and I'm unsure which method would be considered best practice. – BBurchfield Oct 10 '19 at 03:34