Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package me.parzibyte.demostracionrecyclerview;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8.  
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.recyclerview.widget.DividerItemDecoration;
  11. import androidx.recyclerview.widget.LinearLayoutManager;
  12. import androidx.recyclerview.widget.RecyclerView;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. Button btnAgregar = findViewById(R.id.btnAgregar);
  22. Button btnEliminar = findViewById(R.id.btnEliminar);
  23. RecyclerView recyclerViewMascotas = findViewById(R.id.recyclerViewMascotas);
  24. final EditText editTextNombre = findViewById(R.id.editTextNombre);
  25. final EditText editTextEdad = findViewById(R.id.editTextEdad);
  26. final EditText editTextIndice = findViewById(R.id.editTextIndice);
  27.  
  28. final AdaptadorRecyclerView adaptadorRecyclerView = new AdaptadorRecyclerView(new InterfazClickRecyclerView() {
  29. @Override
  30. public void onClick(View v, Mascota m) {
  31. Toast.makeText(MainActivity.this, m.toString(), Toast.LENGTH_SHORT).show();
  32. }
  33. });
  34. // Configuramos cómo se van a organizar las vistas dentro del RecyclerView; simplemente es un LinearLayout para que
  35. // aparezcan una debajo de otra
  36. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
  37. recyclerViewMascotas.setLayoutManager(linearLayoutManager);
  38. // La línea que divide los elementos
  39. recyclerViewMascotas.addItemDecoration(new DividerItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL));
  40. // El adaptador que se encarga de toda la lógica
  41. recyclerViewMascotas.setAdapter(adaptadorRecyclerView);
  42. // adaptadorRecyclerView.agregarMascota(new Mascota());
  43.  
  44. btnAgregar.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. String nombre = editTextNombre.getText().toString();
  48. String edad = editTextEdad.getText().toString();
  49. if (nombre.isEmpty() || edad.isEmpty()) {
  50. Toast.makeText(MainActivity.this, "Rellena los campos", Toast.LENGTH_SHORT).show();
  51. return;
  52. }
  53. adaptadorRecyclerView.agregarMascota(new Mascota(nombre, Integer.valueOf(edad)));
  54. }
  55. });
  56.  
  57. btnEliminar.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View v) {
  60. String indice = editTextIndice.getText().toString();
  61. if (indice.isEmpty()) {
  62. Toast.makeText(MainActivity.this, "Escribe el índice", Toast.LENGTH_SHORT).show();
  63. return;
  64. }
  65. adaptadorRecyclerView.eliminar(Integer.valueOf(indice));
  66. }
  67. });
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement