Acceder

Ver la Versión Completa : DatePicker/Timepicker en fragments


Androidsc
23/09/13, 17:51:13
Buenas,

Llevo varios días intentando meter los pickers para la fecha y la hora en mi aplicación y no consigo sacarlo. El código que tengo es:


public class NuevoGastoFragment extends Fragment{

static final int DATE_DIALOG_ID = 1;
static final int TIME_DIALOG_ID = 2;
private TextView dateDisplay;
private Button pickDate;
private int year, month, day;
private TextView timeDisplay;
private Button pickTime;
private int hours, min;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

dateDisplay = (TextView)findViewById(R.id.TextView01);
pickDate = (Button)findViewById(R.id.Button01);

pickDate.setOnClickListener( new OnClickListener() {

@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}

});

final Calendar cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);

updateDate();

timeDisplay = (TextView)findViewById(R.id.TextView02);
pickTime = (Button)findViewById(R.id.Button02);

pickTime.setOnClickListener( new OnClickListener () {

@Override
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);

}

});

hours = cal.get(Calendar.HOUR);
min = cal.get(Calendar.MINUTE);

updateTime();

return inflater.inflate(R.layout.nuevogastofragment, container, false);
}


private void updateTime() {
timeDisplay.setText(new StringBuilder().append(hours).append(':')
.append(min));

}

private void updateDate() {
dateDisplay.setText(new StringBuilder().append(day).append('-')
.append(month + 1).append('-').append(year));

}

private DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int yr, int monthOfYear,
int dayOfMonth) {
year = yr;
month = monthOfYear;
day = dayOfMonth;
updateDate();
}
};

private TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hours = hourOfDay;
min = minute;
updateTime();
}

};
protected Dialog onCreateDialog(int id){
switch(id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(getActivity(), dateListener, year, month, day);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeListener, hours, min, false);
}
return null;

}
}


}

Me sale error en el showDialog pone "showDialog is undefined for view.onclicklistener()".

Creo que el problema es que estoy trabajando con fragments.

¿Alguien puede echarme una mano?

Mil Gracias. saludos

tompad28
23/09/13, 18:03:58
Prueba poniendo NuevoGastoFragment.this.showDialog(...) dentro del onClick, a ver si por eso.

Androidsc
23/09/13, 18:16:56
Prueba poniendo NuevoGastoFragment.this.showDialog(...) dentro del onClick, a ver si por eso.

Buenas,

Gracias por responder.

Si pongo eso en el ShowDialog me salta error "The method showDialog(int) is undefined for the type NuevoGastoFragment"

-___-

¿A alguien se le ocurre algo?

kriogeN
23/09/13, 19:41:33
¿Dónde está el Dialog? Estás metiendo el código de creación de un Dialog dentro de un Fragment, eso está mal.

El Dialog tiene que ser un objeto propio, en este caso como lo creas desde un Fragment debe ser un DialogFragment.

Androidsc
23/09/13, 22:59:04
Buenas noches Kriogen.

Esta era la otra opción que había probado, me compila bien pero luego en ejecución da error. Creo una clase que extiende DialogFragment y luego desde NuevoGastoFragment la intento abrir creando el objeto de esa clase y con un show. Los errores que da son estos:

09-23 22:55:58.940: D/AndroidRuntime(8557): Shutting down VM
09-23 22:55:58.940: W/dalvikvm(8557): threadid=1: thread exiting with uncaught exception (group=0x41a972a0)
09-23 22:55:58.950: E/AndroidRuntime(8557): FATAL EXCEPTION: main
09-23 22:55:58.950: E/AndroidRuntime(8557): java.lang.IllegalStateException: Could not find a method mostrarDialogoDeTiempo(View) in the activity class com.android.finan.MainActivity for onClick handler on view class android.widget.Button with id 'btDialogTime'
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.view.View$1.onClick(View.java:3678)
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.view.View.performClick(View.java:4211)
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.view.View$PerformClick.run(View.java:17267 )
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.os.Handler.handleCallback(Handler.java:615 )
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.os.Handler.dispatchMessage(Handler.java:92 )
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.os.Looper.loop(Looper.java:137)
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.app.ActivityThread.main(ActivityThread.jav a:4898)
09-23 22:55:58.950: E/AndroidRuntime(8557): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 22:55:58.950: E/AndroidRuntime(8557): at java.lang.reflect.Method.invoke(Method.java:511)
09-23 22:55:58.950: E/AndroidRuntime(8557): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:1006)
09-23 22:55:58.950: E/AndroidRuntime(8557): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:773)
09-23 22:55:58.950: E/AndroidRuntime(8557): at dalvik.system.NativeStart.main(Native Method)
09-23 22:55:58.950: E/AndroidRuntime(8557): Caused by: java.lang.NoSuchMethodException: mostrarDialogoDeTiempo [class android.view.View]
09-23 22:55:58.950: E/AndroidRuntime(8557): at java.lang.Class.getConstructorOrMethod(Class.java: 460)
09-23 22:55:58.950: E/AndroidRuntime(8557): at java.lang.Class.getMethod(Class.java:915)
09-23 22:55:58.950: E/AndroidRuntime(8557): at android.view.View$1.onClick(View.java:3671)
09-23 22:55:58.950: E/AndroidRuntime(8557): ... 11 more


¿Que puede pasar?

Gracias!!!

Androidsc
26/09/13, 15:01:32
Kriogen, please, mira a ver si sabes decirme cual es el fallo.

Muchas gracias y saludos.

Androidsc
05/11/13, 16:06:37
Alguien puede ayudarme?

Aun no lo consegui...U_U