'AlertDialog NumberPicker that opens after a TimePicker is set
I'm trying to implement a functionality such that after the user sets a time (from a TimePicker) I need the user to supply a number (between 1 and 24) that will be stored in a variable. The TimePicker is already implemented:
public class MedicationActivity extends AppCompatActivity {
Button setA;
TextView textA;
TimePickerDialog pickerA;
String filename = "pills";
Calendar calendar = Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medication);
textA = (TextView) findViewById(R.id.alarmprompt);
setA = (Button) findViewById(R.id.setA);
setA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View a) {
textA.setText("");
pickerA = new TimePickerDialog(MedicationActivity.this,
onTimeSetListenerA,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
true);
pickerA.setTitle("Set Pill Time");
pickerA.show();
}
});
TimePickerDialog.OnTimeSetListener onTimeSetListenerA = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
Context context = getApplicationContext();
File file = new File(context.getExternalFilesDir(null).getAbsolutePath(), filename + "A.txt");
Log.d("Logging to", String.valueOf(file));
int month = calSet.get(Calendar.MONTH)+1;
int day = calSet.get(Calendar.DAY_OF_MONTH);
String data = calSet.get(Calendar.HOUR_OF_DAY) + ","
+calSet.get(Calendar.MINUTE) + ","
+day+ "," + month + ","
+calSet.get(Calendar.YEAR) + ",";
textA.setText(+ calSet.get(Calendar.HOUR_OF_DAY) + ":"+ calSet.get(Calendar.MINUTE) +" \n\n\n "+ day +"." + month+"."+calSet.get(Calendar.YEAR));
try {
FileOutputStream stream = new FileOutputStream(file, false);
try {
stream.write(data.getBytes());
}catch (Exception e){
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
};
From my understanding, I have to implement an AlertDialog to pop-up, that contains a NumberPicker. The problem is that I have tried several methods and failed to implement the alert dialog.
Aspects worth mentioning:
This is not the MainActivity it is a child activity.
When I tried to implement a NumberPickerDialogFragment and add the
public class MedicationActivity extends AppCompatActivity implements NumberPickerDialogFragment
there was a conflict between onStart() of the AppCompatActivity and onStart() of the NumberPickerDialogFragment.
Can I get a helping hand, please?
UPDATE 1:
added the following code:
public void showNumberPicker(View view){
NumberPickerDialog newFragment = new NumberPickerDialog();
newFragment.setValueChangeListener(newFragment.getValueChangeListener());
newFragment.show(getSupportFragmentManager(), "time picker");
}
TimePickerDialog.OnTimeSetListener onTimeSetListenerA = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setContentView(R.layout.number_picker_dialog);
showNumberPicker(view);
The NumberPicker opens in a DialogFragment but after number picking and press ok, the app Crashes
Update 2:
The main suspect is this line:
newFragment.setValueChangeListener(newFragment.getValueChangeListener());
I suspect that newFragment.getValueChangeListener() because I get this when the app crashes:
The other posts I have seen use a this instead of newFragment.getValueChangeListener()
Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
