Merche300
06/11/16, 08:14:26
Tengo un problema con el TimePicker, y es que cuando los minutos son inferiores a 10 me sale solo un digito, si son la 10:05 horas pues me muestra las 10:5, ¿como lo puedo solucionar?
public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener,
DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener, View.OnClickListener {
//Declaring an Spinner
private Spinner spinner;
//An ArrayList for Spinner Items
private ArrayList<String> empleados;
//JSON Array
private JSONArray result;
//TextViews to display details
private TextView textViewNombre;
private TextView textViewNacimiento;
private TextView textDni;
private TextView textViewDesde;
Button btnInicio, btnFin;
TextView txtFechaInicio, txtHoraInicio;
TextView txtFechaFin, txtHoraFin;
private int mYear, mMonth, mDay, mHour, mMinute;
override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInicio=(Button)findViewById(R.id.btn_inicio);
btnFin=(Button)findViewById(R.id.btn_fin);
txtFechaInicio=(TextView)findViewById(R.id.tv_fech ain);
txtHoraInicio=(TextView)findViewById(R.id.tv_horai n);
txtFechaFin=(TextView)findViewById(R.id.tv_fechafi n);
txtHoraFin=(TextView)findViewById(R.id.tv_horafin) ;
btnInicio.setOnClickListener(this);
btnFin.setOnClickListener(this);
//Initializing the ArrayList
empleados = new ArrayList<String>();
//Initializing Spinner
spinner = (Spinner) findViewById(R.id.spinner);
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
spinner.setOnItemSelectedListener(this);
//Initializing TextViews
textViewNombre = (TextView) findViewById(R.id.tv_nombre);
textViewNacimiento = (TextView) findViewById(R.id.tv_nacimiento);
textDni = (TextView) findViewById(R.id.tv_dni);
textViewDesde = (TextView) findViewById(R.id.tv_desde);
//This method will fetch the data from the URL
getData();
}
public void onClick(View v) {
if (v == btnInicio) {
// Get Current Date
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtFechaInicio.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
// Get Current Time
final Calendar timeIni = Calendar.getInstance();
mHour = timeIni.get(Calendar.HOUR_OF_DAY);
mMinute = timeIni.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtHoraInicio.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
if (v == btnFin) {
// Get Current Date
final Calendar dateFin = Calendar.getInstance();
mYear = dateFin.get(Calendar.YEAR);
mMonth = dateFin.get(Calendar.MONTH);
mDay = dateFin.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtFechaFin.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
// Get Current Time
final Calendar timeFin = Calendar.getInstance();
mHour = timeFin.get(Calendar.HOUR_OF_DAY);
mMinute = timeFin.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtHoraFin.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the empleados from the JSON Array
getStudents(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getStudents(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
empleados.add(json.getString(Config.TAG_USERNAME)) ;
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, empleados));
}
//Method to get student name of a particular position
private String getNombre(int position){
String name="";
try {
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching name from that object
name = json.getString(Config.TAG_NOMBRE);
} catch (JSONException e) {
e.printStackTrace();
}
//Returning the name
return name;
}
private String getNacimiento(int position){
String nacimiento="";
try {
JSONObject json = result.getJSONObject(position);
nacimiento = json.getString(Config.TAG_NACIMIENTO);
} catch (JSONException e) {
e.printStackTrace();
}
return nacimiento;
}
private String getDni(int position){
String dni="";
try {
JSONObject json = result.getJSONObject(position);
dni = json.getString(Config.TAG_DNI);
} catch (JSONException e) {
e.printStackTrace();
}
return dni;
}
private String getDesde(int position){
String desde="";
try {
JSONObject json = result.getJSONObject(position);
desde = json.getString(Config.TAG_DESDE);
} catch (JSONException e) {
e.printStackTrace();
}
return desde;
}
//this method will execute when we pic an item from the spinner
override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
textViewNombre.setText(getNombre(position));
textViewNacimiento.setText(getNacimiento(position) );
textDni.setText(getDni(position));
textViewDesde.setText(getDesde(position));
}
//When no item is selected this method would execute
override
public void onNothingSelected(AdapterView<?> parent) {
textViewNombre.setText("");
textViewNacimiento.setText("");
textDni.setText("");
textViewDesde.setText("");
}
override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(
this,
"Fecha: " + year + "-" + monthOfYear + "-" + dayOfMonth,
Toast.LENGTH_LONG)
.show();
}
override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(
this,
"Tiempo: " + hourOfDay + ":" + minute,
Toast.LENGTH_LONG)
.show();
}
Gracias
public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener,
DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener, View.OnClickListener {
//Declaring an Spinner
private Spinner spinner;
//An ArrayList for Spinner Items
private ArrayList<String> empleados;
//JSON Array
private JSONArray result;
//TextViews to display details
private TextView textViewNombre;
private TextView textViewNacimiento;
private TextView textDni;
private TextView textViewDesde;
Button btnInicio, btnFin;
TextView txtFechaInicio, txtHoraInicio;
TextView txtFechaFin, txtHoraFin;
private int mYear, mMonth, mDay, mHour, mMinute;
override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnInicio=(Button)findViewById(R.id.btn_inicio);
btnFin=(Button)findViewById(R.id.btn_fin);
txtFechaInicio=(TextView)findViewById(R.id.tv_fech ain);
txtHoraInicio=(TextView)findViewById(R.id.tv_horai n);
txtFechaFin=(TextView)findViewById(R.id.tv_fechafi n);
txtHoraFin=(TextView)findViewById(R.id.tv_horafin) ;
btnInicio.setOnClickListener(this);
btnFin.setOnClickListener(this);
//Initializing the ArrayList
empleados = new ArrayList<String>();
//Initializing Spinner
spinner = (Spinner) findViewById(R.id.spinner);
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
spinner.setOnItemSelectedListener(this);
//Initializing TextViews
textViewNombre = (TextView) findViewById(R.id.tv_nombre);
textViewNacimiento = (TextView) findViewById(R.id.tv_nacimiento);
textDni = (TextView) findViewById(R.id.tv_dni);
textViewDesde = (TextView) findViewById(R.id.tv_desde);
//This method will fetch the data from the URL
getData();
}
public void onClick(View v) {
if (v == btnInicio) {
// Get Current Date
final Calendar dateIni = Calendar.getInstance();
mYear = dateIni.get(Calendar.YEAR);
mMonth = dateIni.get(Calendar.MONTH);
mDay = dateIni.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtFechaInicio.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
// Get Current Time
final Calendar timeIni = Calendar.getInstance();
mHour = timeIni.get(Calendar.HOUR_OF_DAY);
mMinute = timeIni.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtHoraInicio.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
if (v == btnFin) {
// Get Current Date
final Calendar dateFin = Calendar.getInstance();
mYear = dateFin.get(Calendar.YEAR);
mMonth = dateFin.get(Calendar.MONTH);
mDay = dateFin.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
txtFechaFin.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
// Get Current Time
final Calendar timeFin = Calendar.getInstance();
mHour = timeFin.get(Calendar.HOUR_OF_DAY);
mMinute = timeFin.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
txtHoraFin.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the empleados from the JSON Array
getStudents(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getStudents(JSONArray j){
//Traversing through all the items in the json array
for(int i=0;i<j.length();i++){
try {
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
empleados.add(json.getString(Config.TAG_USERNAME)) ;
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, empleados));
}
//Method to get student name of a particular position
private String getNombre(int position){
String name="";
try {
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching name from that object
name = json.getString(Config.TAG_NOMBRE);
} catch (JSONException e) {
e.printStackTrace();
}
//Returning the name
return name;
}
private String getNacimiento(int position){
String nacimiento="";
try {
JSONObject json = result.getJSONObject(position);
nacimiento = json.getString(Config.TAG_NACIMIENTO);
} catch (JSONException e) {
e.printStackTrace();
}
return nacimiento;
}
private String getDni(int position){
String dni="";
try {
JSONObject json = result.getJSONObject(position);
dni = json.getString(Config.TAG_DNI);
} catch (JSONException e) {
e.printStackTrace();
}
return dni;
}
private String getDesde(int position){
String desde="";
try {
JSONObject json = result.getJSONObject(position);
desde = json.getString(Config.TAG_DESDE);
} catch (JSONException e) {
e.printStackTrace();
}
return desde;
}
//this method will execute when we pic an item from the spinner
override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Setting the values to textviews for a selected item
textViewNombre.setText(getNombre(position));
textViewNacimiento.setText(getNacimiento(position) );
textDni.setText(getDni(position));
textViewDesde.setText(getDesde(position));
}
//When no item is selected this method would execute
override
public void onNothingSelected(AdapterView<?> parent) {
textViewNombre.setText("");
textViewNacimiento.setText("");
textDni.setText("");
textViewDesde.setText("");
}
override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(
this,
"Fecha: " + year + "-" + monthOfYear + "-" + dayOfMonth,
Toast.LENGTH_LONG)
.show();
}
override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(
this,
"Tiempo: " + hourOfDay + ":" + minute,
Toast.LENGTH_LONG)
.show();
}
Gracias