Advertisement
eni86

AgregarAnimalActivity.java

Apr 18th, 2021 (edited)
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1. public class AgregarAnimalActivity extends AppCompatActivity {
  2.     public static final String EXTRA_MSG_ID ="com.example.fruteria.MSG_GUARDAR ID";
  3.     public static final String EXTRA_MSG_CARAVANA ="com.example.fruteria.MSG_GUARDAR CARAVANA";
  4.     public static final String EXTRA_MSG_RAZA ="com.example.fruteria.MSG_GUARDAR RAZA";
  5.     public static final String EXTRA_MSG_SEXO = "com.example.fruteria.MSG_GUARDAR SEXO";
  6. //    private static final int RECOGNIZE_SPEECH_ACTIVITY = 1;
  7.     private EditText editTextCaravana;
  8.  
  9.     private Spinner spinnerRaza;
  10.  
  11.     private Spinner spinnerCategoria;
  12.  
  13.     private String sexo="Indefinido";
  14.     private RadioGroup radioGroup;
  15.     private RadioButton radioButton;
  16.  
  17.     EditText fechaNacimiento;
  18.  
  19.     private EditText editTextCaravanaPadre;
  20.     private EditText editTextCaravanaMadre;
  21.  
  22.     private EditText editTextPotrero;
  23.  
  24.     private Animal animal;
  25.     private AnimalRepository animalRepository;
  26.  
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_agregar_animal);
  31.         editTextCaravana = (EditText) findViewById(R.id.caravana);
  32.         editTextPotrero = (EditText) findViewById(R.id.potrero);
  33.  
  34.         editTextCaravanaPadre= (EditText) findViewById(R.id.caravanaPadre);
  35.         editTextCaravanaMadre= (EditText) findViewById(R.id.caravanaMadre);
  36.  
  37.         fechaNacimiento = (EditText) findViewById(R.id.txtViewNewAnimalNacimiento);
  38.         fechaNacimiento.setOnClickListener((View.OnClickListener) this);
  39.         /*sexo*/
  40.         radioGroup=findViewById(R.id.machoOHembra);
  41.         /* Esto es para el spinner de la raza*/
  42.         spinnerRaza = (Spinner) findViewById(R.id.SpinnerRaza);
  43.         /* Esto es para el spinner de la categoria*/
  44.         spinnerCategoria = (Spinner) findViewById(R.id.SpinnerCategoria);
  45.  
  46.         try {
  47.             ArrayList <String> razas= new ArrayList<>();
  48.             razas=(ArrayList) Raza.toArray();
  49.             ArrayAdapter adapter = new ArrayAdapter(AgregarAnimalActivity.this, android.R.layout.simple_spinner_dropdown_item, razas);
  50.             spinnerRaza.setAdapter(adapter);
  51.  
  52.             //Esto es para el spinner da la categoría
  53.             ArrayList <String> categoria= new ArrayList<>();
  54.             categoria=(ArrayList) Categoria.toArray();
  55.             adapter = new ArrayAdapter(AgregarAnimalActivity.this, android.R.layout.simple_spinner_dropdown_item, categoria);
  56.             spinnerCategoria.setAdapter(adapter);
  57.  
  58.         }catch(Exception e){
  59.             Log.v("DEENI",e.getMessage()+" "+ String.valueOf(Categoria.values().length));
  60.         }
  61.  
  62.         //This is an CUT AND PASTE TOTALLLLL
  63.         new Thread(new Runnable() {
  64.             @Override
  65.             public void run() {
  66.                 try {
  67.                     animalRepository = new AnimalRepository(getApplication());
  68.                 } catch (Exception e) {
  69.                     Log.v("DEENI", "Thread error" + e.getMessage());
  70.                 }
  71.             }
  72.         }).start();
  73.  
  74.  
  75.  
  76.         }
  77.  
  78.     public String elegirSexo(View v){
  79.         int radioId = radioGroup.getCheckedRadioButtonId();
  80.         radioButton= findViewById(radioId);
  81.         sexo=radioButton.getText().toString();
  82.         return sexo;
  83.     }
  84.  
  85.     //Agrega al nuevo animal
  86.     public void agregarAnimal(View v){
  87.         animal = new Animal();
  88.         try {
  89.             animal.setCaravana(Integer.parseInt(editTextCaravana.getText().toString()));
  90.             Log.v("DEENI","Veo getcaravana"+animal.getCaravana());
  91.  
  92.  
  93.             //Log.v("DEENI","Elegir Sexo");
  94.             //si no haya la caravana sigue asignando el resto de los campos e inserta al animal nuevo
  95.             //if (animalRepository.findByCaravana(animal.getCaravana()) == null){
  96.                 try {
  97.                     animal.setRaza(spinnerRaza.getSelectedItem().toString());
  98.                     Log.v("DEENI","Seteo de raza");
  99.                 } catch (Exception e){
  100.                     Log.v("DEENI","No pudo Setear la raza");
  101.                 }
  102.             try {
  103.                 animal.setSexo(sexo);
  104.                 Log.v("DEENI","Seteo de sexo");
  105.             } catch (Exception e){
  106.                 Log.v("DEENI","No pudo Setear sexo");
  107.             }
  108.                 try {
  109.                     animal.setCategoria(spinnerCategoria.getSelectedItem().toString());
  110.                     Log.v("DEENI","Seteo de Categoria");
  111.                 } catch (Exception e){
  112.                     Log.v("DEENI","No pudo Setear la categoria");
  113.                 }
  114.  
  115.                 try {
  116.                     animal.setPadre(Integer.parseInt(editTextCaravanaPadre.getText().toString()));
  117.                     Log.v("DEENI","Seteo de Padre");
  118.                 } catch (Exception e){
  119.                     Log.v("DEENI","No pudo Setear el padre");
  120.                     animal.setPadre(0);
  121.                 }
  122.  
  123.                 try {
  124.                     animal.setMadre(Integer.parseInt(editTextCaravanaMadre.getText().toString()));
  125.                     Log.v("DEENI","Seteo de Madre");
  126.                 } catch (Exception e){
  127.                     Log.v("DEENI","No pudo Setear la madre");
  128.                     animal.setMadre(0);
  129.                 }
  130.  
  131.                 try {
  132.                     animal.setPotrero(Integer.parseInt(editTextPotrero.getText().toString()));
  133.                     Log.v("DEENI","Seteo de Potrero");
  134.                 } catch (Exception e){
  135.                     Log.v("DEENI","No pudo Setear el potrero");
  136.                 }
  137.  
  138.                 try {
  139.                     animal.setSexo(sexo);
  140.                     Log.v("DEENI","Seteo de Sexo");
  141.  
  142.  
  143.                 try {
  144.                     Log.v("DEENI","Seteo de fecha nacimiento");
  145.                     String fecha = fechaNacimiento.getText().toString();
  146.                     List<String> fechaAr = null;
  147.                     Integer dia, mes, anio;
  148.  
  149.                     if (fecha != null) {
  150.                         fechaAr = asList(fecha.split("/"));
  151.                         dia = Integer.valueOf(fechaAr.get(0));
  152.                         mes = Integer.valueOf(fechaAr.get(1));
  153.                         anio = Integer.valueOf(fechaAr.get(2));
  154.  
  155.                     } else {
  156.                         dia = 1;
  157.                         mes = 1;
  158.                         anio = 1970;
  159.                         Toast.makeText(this, "Fecha incorrecta", Toast.LENGTH_SHORT);
  160.                         return;
  161.                     }
  162.                     Calendar c = Calendar.getInstance();
  163.                     c.set(anio, mes, dia);
  164.                     animal.setFechaNacimiento(dia.toString()+"/"+mes.toString()+"/"+anio.toString());
  165.                 } catch (Exception e){
  166.                     Log.v("DEENI","No pudo setear la fecha de nacimiento");
  167.                 }
  168.  
  169.  
  170.  
  171.  
  172.                 Log.v("DEENI", "LLamo insert animal");
  173.                 if (animal != null) {
  174.                     Log.v("DEENI", "Caravana:" + animal.getCaravana());
  175.                     animalRepository.insert(animal); //Esto lo manda a la BD
  176.                     Toast.makeText(getApplicationContext(),"El animal fué guardado con éxito",Toast.LENGTH_LONG);
  177.                 } else {
  178.                     Log.v("DEENI", "Horror 2!!!, animal es null ");
  179.                 }
  180.             //} else {
  181.             //    Toast.makeText(getApplicationContext(),"El animal ya se encuentra en la base de datos",Toast. LENGTH_SHORT);
  182.             //}
  183.         } catch (Exception e) {
  184.             Log.v("DEENI", "Horror!!! " + e.getMessage());
  185.         }
  186.     } catch (Exception e) {
  187.             Log.v("DEENI", "El primer try no funciono " + e.getMessage());
  188.         }
  189.  
  190.     }
  191.  
  192.     @Override
  193.     public void onClick(View v) {
  194.         switch (v.getId()) {
  195.             case R.id.txtViewNewAnimalNacimiento:
  196.                 showDatePickerDialog();
  197.                 break;
  198.             case R.id.machoOHembra:
  199.                 sexo = elegirSexo(v);
  200.         }
  201.     }
  202.  
  203.  
  204.     //https://programacionymas.com/blog/como-pedir-fecha-android-usando-date-picker
  205.     private void showDatePickerDialog() {
  206.         DatePickerFragment newFragment = DatePickerFragment.newInstance(new DatePickerDialog.OnDateSetListener() {
  207.             @Override
  208.             public void onDateSet(DatePicker datePicker, int year, int month, int day) {
  209.                 // +1 because January is zero
  210.                 final String selectedDate = day + " / " + (month+1) + " / " + year;
  211.                 fechaNacimiento.setText(selectedDate);
  212.             }
  213.         });
  214.  
  215.         newFragment.show(getSupportFragmentManager(), "datePicker");
  216.     }
  217.  
  218.  
  219.  
  220.  
  221.  
  222.     /* metodo para volver de Al menú de animales */
  223.     public void volverAMenuanimal (View view){
  224.         Intent intent = new Intent(this, AnimalMenuActivity.class);
  225.         startActivity(intent);
  226.         finish();
  227.     }
  228.  
  229.     public void volverAMenuPrincipal (View view){
  230.         Intent intent = new Intent(this, MenuPrincipal.class);
  231.         startActivity(intent);
  232.         finish();
  233.     }
  234.  
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement