Tengo 2 clases llamadas MyAdapterBig/MyAdapterSmall, representan dos frigos. El tema es que uno de ellos el small va perfectamente, cuando le doy al boton de delete borra, pero el otro Adapter, no entra en el for... entonces... me estoy volviendo loco.
Alguien puede ver el fallo que yo no consigo ver ??
MyAdapterBig, este no va, no se porque razon nunca entra aqui:
for (DataSnapshot prodSnapshot : dataSnapshot.getChildren()) {
Lo que no se porque...
package com.virtualmon.sergisabgg.whateattoday;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
class MyAdapterBig extends RecyclerView.Adapter<MyAdapterBig.MyViewHolder> {
private ArrayList<Productos> frigos;
public MyAdapterBig(ArrayList<Productos> frigos) {
this.frigos = frigos;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_big, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
Productos object = frigos.get(position);
String firstText = object.getNombre();
holder.text1.setText(firstText);
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = holder.text1.getText().toString();
holder.myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("SDASDASDASDASDASD ", "" + dataSnapshot.getChildren().toString());
for (DataSnapshot prodSnapshot : dataSnapshot.getChildren()) {
Log.d("SDASDASDASDASDASD ", "" + "12312313123123123123");
if (prodSnapshot.getValue(Productos.class).getNombre().equals(name)) {
Productos prod = prodSnapshot.getValue(Productos.class);
prodSnapshot.getRef().removeValue();
Log.d("Delete:", prod.toString());
frigos.remove(prod);
notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Error", "Failed to read value.", error.toException());
}
});
}
});
}
@Override
public int getItemCount() {
return frigos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView text1;
Button delete;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("bigFrigos");
MyViewHolder(View itemView) {
super(itemView);
text1 = (TextView) itemView.findViewById(R.id.text1);
delete = (Button) itemView.findViewById(R.id.delete);
}
}
}
En cambio este funciona de maravilla....
package com.virtualmon.sergisabgg.whateattoday;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
class MyAdapterSmall extends RecyclerView.Adapter<MyAdapterSmall.MyViewHolder> {
private ArrayList<Productos> frigos;
public MyAdapterSmall(ArrayList<Productos> arrayList) {
frigos = arrayList;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
Productos object = frigos.get(position);
String firstText = object.getNombre();
holder.text1.setText(firstText);
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = holder.text1.getText().toString();
holder.myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot prodSnapshot : dataSnapshot.getChildren()) {
if (prodSnapshot.getValue(Productos.class).getNombre().equals(name)) {
Productos prod = prodSnapshot.getValue(Productos.class);
prodSnapshot.getRef().removeValue();
Log.d("Delete:", prod.toString());
frigos.remove(prod);
notifyDataSetChanged();
}
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Error", "Failed to read value.", error.toException());
}
});
}
});
}
@Override
public int getItemCount() {
return frigos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView text1;
Button delete;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("smallFrigo");
MyViewHolder(View itemView) {
super(itemView);
text1 = (TextView) itemView.findViewById(R.id.text1);
delete = (Button) itemView.findViewById(R.id.delete);
}
}
}
Muchas gracias. Un cordial saludo.