-3

Here is my code for a TimePicker class I have created

picker.java

package com.example.abhijeet.todo;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;

/**
 * Created by Abhijeet on 9/6/2017.
 */

public class Picker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }



    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        //Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
       View v =View.inflate(*  ?   *,R.layout.activity_editor,null);  //HERE
        //Displaying the user selected time on the Edit Text view
        EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);


    }
}

I am trying to show the time selected via the picker in a edit text box. I am not able to find the right context for the argument.

I have read the answer here which explains about the different usages of the context, but that does not seem to help me.

Thank for the help in Advance.

  • create constructor for Picker class having Context parameter – Bapusaheb Shinde Sep 06 '17 at 11:40
  • Hi, thanks for the help but I'm afraid I've tried that already. It says " Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)" Does it makes sense to you @BapusahebShinde – Abhijeet Rathore Sep 06 '17 at 11:50

2 Answers2

0

Just call getContext()

As DialogFragment is child of Fragment. So all the Fragment class method could be called in DialogFragment.

Like this

@Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        //Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
       View v =View.inflate(getContext() ,R.layout.activity_editor,null);  //HERE
        //Displaying the user selected time on the Edit Text view
        EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
    }

You can also call getActivity() method. If you are targeting to older API than 23:

@Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        //Creating a View object because this class do not extend from activity class so no findviewbyid() method is present
       View v =View.inflate(getActivity() ,R.layout.activity_editor,null);  //HERE
        //Displaying the user selected time on the Edit Text view
        EditText edittime = (EditText) v.findViewById(R.id.time_editext_field);
    }
Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54
  • Thank you It works but shows a warning " Call requires API level 23 (current min is 21)". How can i make it supported on lower API devices while still maintaining this implementation ? – Abhijeet Rathore Sep 06 '17 at 12:02
  • Wow, I have been struck at this for about an hour or so. I did try to read the documentation, what is the correct procedure for searching issues like this as i am quite sure this won't be the last and I am likely to get banned once again from asking questions Meh. – Abhijeet Rathore Sep 06 '17 at 12:11
  • you should use appcompat fragments, then you could use getContext everywhere – Zharf Sep 06 '17 at 12:12
0

Whenever you are used. Activity -> getApplicationContext(), Fragment -> getActivity(), Adapter -> getContext()

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
Arjun Solanki
  • 236
  • 1
  • 11