Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AgregarAnimalActivity extends AppCompatActivity {
- public static final String EXTRA_MSG_ID ="com.example.fruteria.MSG_GUARDAR ID";
- public static final String EXTRA_MSG_CARAVANA ="com.example.fruteria.MSG_GUARDAR CARAVANA";
- public static final String EXTRA_MSG_RAZA ="com.example.fruteria.MSG_GUARDAR RAZA";
- public static final String EXTRA_MSG_SEXO = "com.example.fruteria.MSG_GUARDAR SEXO";
- // private static final int RECOGNIZE_SPEECH_ACTIVITY = 1;
- private EditText editTextCaravana;
- private Spinner spinnerRaza;
- private Spinner spinnerCategoria;
- private String sexo="Indefinido";
- private RadioGroup radioGroup;
- private RadioButton radioButton;
- EditText fechaNacimiento;
- private EditText editTextCaravanaPadre;
- private EditText editTextCaravanaMadre;
- private EditText editTextPotrero;
- private Animal animal;
- private AnimalRepository animalRepository;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_agregar_animal);
- editTextCaravana = (EditText) findViewById(R.id.caravana);
- editTextPotrero = (EditText) findViewById(R.id.potrero);
- editTextCaravanaPadre= (EditText) findViewById(R.id.caravanaPadre);
- editTextCaravanaMadre= (EditText) findViewById(R.id.caravanaMadre);
- fechaNacimiento = (EditText) findViewById(R.id.txtViewNewAnimalNacimiento);
- fechaNacimiento.setOnClickListener((View.OnClickListener) this);
- /*sexo*/
- radioGroup=findViewById(R.id.machoOHembra);
- /* Esto es para el spinner de la raza*/
- spinnerRaza = (Spinner) findViewById(R.id.SpinnerRaza);
- /* Esto es para el spinner de la categoria*/
- spinnerCategoria = (Spinner) findViewById(R.id.SpinnerCategoria);
- try {
- ArrayList <String> razas= new ArrayList<>();
- razas=(ArrayList) Raza.toArray();
- ArrayAdapter adapter = new ArrayAdapter(AgregarAnimalActivity.this, android.R.layout.simple_spinner_dropdown_item, razas);
- spinnerRaza.setAdapter(adapter);
- //Esto es para el spinner da la categoría
- ArrayList <String> categoria= new ArrayList<>();
- categoria=(ArrayList) Categoria.toArray();
- adapter = new ArrayAdapter(AgregarAnimalActivity.this, android.R.layout.simple_spinner_dropdown_item, categoria);
- spinnerCategoria.setAdapter(adapter);
- }catch(Exception e){
- Log.v("DEENI",e.getMessage()+" "+ String.valueOf(Categoria.values().length));
- }
- //This is an CUT AND PASTE TOTALLLLL
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- animalRepository = new AnimalRepository(getApplication());
- } catch (Exception e) {
- Log.v("DEENI", "Thread error" + e.getMessage());
- }
- }
- }).start();
- }
- public String elegirSexo(View v){
- int radioId = radioGroup.getCheckedRadioButtonId();
- radioButton= findViewById(radioId);
- sexo=radioButton.getText().toString();
- return sexo;
- }
- //Agrega al nuevo animal
- public void agregarAnimal(View v){
- animal = new Animal();
- try {
- animal.setCaravana(Integer.parseInt(editTextCaravana.getText().toString()));
- Log.v("DEENI","Veo getcaravana"+animal.getCaravana());
- //Log.v("DEENI","Elegir Sexo");
- //si no haya la caravana sigue asignando el resto de los campos e inserta al animal nuevo
- //if (animalRepository.findByCaravana(animal.getCaravana()) == null){
- try {
- animal.setRaza(spinnerRaza.getSelectedItem().toString());
- Log.v("DEENI","Seteo de raza");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear la raza");
- }
- try {
- animal.setSexo(sexo);
- Log.v("DEENI","Seteo de sexo");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear sexo");
- }
- try {
- animal.setCategoria(spinnerCategoria.getSelectedItem().toString());
- Log.v("DEENI","Seteo de Categoria");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear la categoria");
- }
- try {
- animal.setPadre(Integer.parseInt(editTextCaravanaPadre.getText().toString()));
- Log.v("DEENI","Seteo de Padre");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear el padre");
- animal.setPadre(0);
- }
- try {
- animal.setMadre(Integer.parseInt(editTextCaravanaMadre.getText().toString()));
- Log.v("DEENI","Seteo de Madre");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear la madre");
- animal.setMadre(0);
- }
- try {
- animal.setPotrero(Integer.parseInt(editTextPotrero.getText().toString()));
- Log.v("DEENI","Seteo de Potrero");
- } catch (Exception e){
- Log.v("DEENI","No pudo Setear el potrero");
- }
- try {
- animal.setSexo(sexo);
- Log.v("DEENI","Seteo de Sexo");
- try {
- Log.v("DEENI","Seteo de fecha nacimiento");
- String fecha = fechaNacimiento.getText().toString();
- List<String> fechaAr = null;
- Integer dia, mes, anio;
- if (fecha != null) {
- fechaAr = asList(fecha.split("/"));
- dia = Integer.valueOf(fechaAr.get(0));
- mes = Integer.valueOf(fechaAr.get(1));
- anio = Integer.valueOf(fechaAr.get(2));
- } else {
- dia = 1;
- mes = 1;
- anio = 1970;
- Toast.makeText(this, "Fecha incorrecta", Toast.LENGTH_SHORT);
- return;
- }
- Calendar c = Calendar.getInstance();
- c.set(anio, mes, dia);
- animal.setFechaNacimiento(dia.toString()+"/"+mes.toString()+"/"+anio.toString());
- } catch (Exception e){
- Log.v("DEENI","No pudo setear la fecha de nacimiento");
- }
- Log.v("DEENI", "LLamo insert animal");
- if (animal != null) {
- Log.v("DEENI", "Caravana:" + animal.getCaravana());
- animalRepository.insert(animal); //Esto lo manda a la BD
- Toast.makeText(getApplicationContext(),"El animal fué guardado con éxito",Toast.LENGTH_LONG);
- } else {
- Log.v("DEENI", "Horror 2!!!, animal es null ");
- }
- //} else {
- // Toast.makeText(getApplicationContext(),"El animal ya se encuentra en la base de datos",Toast. LENGTH_SHORT);
- //}
- } catch (Exception e) {
- Log.v("DEENI", "Horror!!! " + e.getMessage());
- }
- } catch (Exception e) {
- Log.v("DEENI", "El primer try no funciono " + e.getMessage());
- }
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.txtViewNewAnimalNacimiento:
- showDatePickerDialog();
- break;
- case R.id.machoOHembra:
- sexo = elegirSexo(v);
- }
- }
- //https://programacionymas.com/blog/como-pedir-fecha-android-usando-date-picker
- private void showDatePickerDialog() {
- DatePickerFragment newFragment = DatePickerFragment.newInstance(new DatePickerDialog.OnDateSetListener() {
- @Override
- public void onDateSet(DatePicker datePicker, int year, int month, int day) {
- // +1 because January is zero
- final String selectedDate = day + " / " + (month+1) + " / " + year;
- fechaNacimiento.setText(selectedDate);
- }
- });
- newFragment.show(getSupportFragmentManager(), "datePicker");
- }
- /* metodo para volver de Al menú de animales */
- public void volverAMenuanimal (View view){
- Intent intent = new Intent(this, AnimalMenuActivity.class);
- startActivity(intent);
- finish();
- }
- public void volverAMenuPrincipal (View view){
- Intent intent = new Intent(this, MenuPrincipal.class);
- startActivity(intent);
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement