I have an application with a recyclerview made with cardviews, and inside the cardview I have a textview where I store the likes that I give to each pet. The problem is that I can not get the connection between the adapter class and the class in which I receive the data.
Example:
I have a button inside a toolbar, that when I have only implemented the intent without "putExtra" it works and it takes me to the next activity, but as soon as I try to pass data I have an error, so the application falls as soon as I click on said button. How can I pass the information of the likes of my textview that I have inside the cardview when I click on the button that is in the toolbar? Will I have to create another different adapter for the next activity? To not put as much code I put the most important in which you may have the problem.
These are the methods that I implement within the onCreate of my MainActivity class:
public void inicializarLista(){
mascotasList = new ArrayList<Mascotas>();
mascotasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
mascotasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
mascotasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
mascotasList.add(new Mascotas(R.drawable.perro,"Mochi"));
mascotasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
}
public MascotasAdapter adapter;
public void inicializarAdaptador(){
adapter = new MascotasAdapter(this, mascotasList);
mascotaRecycler.setAdapter(adapter);
}
public void cliclearBoton(){
ImageButton favoritas = findViewById(R.id.favoritasCinco);
favoritas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView cantLikes = findViewById(R.id.tvCantidad);
String dlikes = cantLikes.getText().toString();
Intent i = new Intent(MainActivity.this, MascotasFavoritas.class);
i.putExtra("loslikes", dlikes);
startActivity(i);
}
});
}
This is my onBindViewHolder method of the MascotaAdapter class:
@Override
public void onBindViewHolder(final MascotasViewHolder mascotasHolder, final int position) {
final Mascotas mascotasList = mascotas.get(position);
mascotasHolder.imFoto.setImageResource(mascotasList.getIvFoto());
mascotasHolder.textNombre.setText(mascotasList.getNombre());
mascotasHolder.ibDarLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v,"Diste like a "+ mascotasList.getNombre(),Snackbar.LENGTH_SHORT).show();
int plusLike = Integer.parseInt(mascotasHolder.textCantidadLikes.getText().toString());
mascotasHolder.textCantidadLikes.setText(String.valueOf(plusLike + 1));
}
});
}
And these are the methods that I have within the onCreate of the receiving class PetsFavorites:
void inicializarFavoritas(){
favoritasList = new ArrayList<Mascotas>();
favoritasList.add(new Mascotas(R.drawable.perro,"Mochi"));
favoritasList.add(new Mascotas(R.drawable.conejo,"Bonny"));
favoritasList.add(new Mascotas(R.drawable.loro,"Coti el loro"));
favoritasList.add(new Mascotas(R.drawable.hamster,"El Ghamster"));
favoritasList.add(new Mascotas(R.drawable.tortuga,"Turtle Ninja"));
}
public MascotasAdapter adapter;
public void inicializarAdaptador(){
adapter = new MascotasAdapter(this, favoritasList);
favoritasRecycler.setAdapter(adapter);
}
public void recibirLikes(){
TextView receptorLikes = findViewById(R.id.tvCantidad);
Bundle parametros = this.getIntent().getExtras();
String datoLike = parametros.getString("loslikes");
receptorLikes.setText(datoLike);
}