RecyclerView持有人职位未更新

时间:2017-04-28 10:12:00

标签: android database firebase android-recyclerview

我正在尝试从Recycler View更新我的Firebase数据库元素。主要问题是喜欢/不喜欢的方法只适用于最后一个RecyclerView持有者。

public class MuroAdapter extends RecyclerView.Adapter<MuroAdapter.CarrerasViewHolder> implements OnSuccessListener<Uri>, OnFailureListener, OnLikeListener{

private final String KEY_RUTA_CORAZONES = "numCorazones";
private final String KEY_RUTA_ESTRELLAS = "numEstrellas";
private final String KEY_RUTA_THUMBS = "numThumbs";

private Context context;
private List<Carrera> listaCarreras;
private FirebaseStorage storage = FirebaseStorage.getInstance();
private StorageReference storageReference = storage.getReferenceFromUrl("_____");
private DatabaseReference rootDatabaseCarrera = FirebaseDatabase.getInstance().getReference().getRoot().child("Carrera");
private CarrerasViewHolder holder;
private Carrera carrera;

public MuroAdapter(List<Carrera> listaCarreras) {
    this.listaCarreras = listaCarreras;
}

@Override
public CarrerasViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.muro_recycler_items, parent, false);
    CarrerasViewHolder carrerasViewHolder = new CarrerasViewHolder(view);
    context = parent.getContext();
    return carrerasViewHolder;
}

@Override
public void onBindViewHolder(CarrerasViewHolder holder, int position) {
    // Obtenemos cada carrera
    this.holder = holder;
    this.carrera = listaCarreras.get(position);

    // Ponemos datos en los placeholders
    holder.tvContadorCorazones.setText(String.valueOf(carrera.getNumCorazones()));
    holder.tvContadorEstrellas.setText(String.valueOf(carrera.getNumEstrellas()));
    holder.tvContadorThumbs.setText(String.valueOf(carrera.getNumThumbs()));
    holder.tvNombreUsuario.setText(carrera.getUsuario());
    Picasso.with(holder.ivFotoPerfil.getContext())
            .load(carrera.getImagenUsuario())
            .resize(480, 480)
            .centerCrop()
            .into(holder.ivFotoPerfil);

    // Obtenemos la imagen de cada carrera
    storageReference
            .child(carrera.getImagenRecorrido())
            .getDownloadUrl()
            .addOnSuccessListener(this)
            .addOnFailureListener(this);

    // Escuchamos a los botones
    holder.btnCorazon.setOnLikeListener(this);
    holder.btnEstrella.setOnLikeListener(this);
    holder.btnThumb.setOnLikeListener(this);

}

@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemCount() {
    return listaCarreras.size();
}

/** OnSucess & OnFailure interface methods **/

@Override
public void onSuccess(Uri uri) {
    // Ponemos la imagen de cada recorrido en su respectivo placeholder
    Picasso.with(holder.ivFotoRecorrido.getContext())
            .load(uri)
            .resize(480, 480)
            .centerCrop()
            .into(holder.ivFotoRecorrido);
}

@Override
public void onFailure(@NonNull Exception e) {
    Toasty.error(context, "Error cargando recorridos", Toast.LENGTH_SHORT).show();
}

/** OnLikeListener interface methods **/

@Override
public void liked(LikeButton likeButton) {
    switch (likeButton.getId()){
        case R.id.heart_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_CORAZONES)
                    .setValue(carrera.getNumCorazones() + 1);
            holder.tvContadorCorazones.setText(String.valueOf(carrera.getNumCorazones()));
            break;
        case R.id.star_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_ESTRELLAS)
                    .setValue(carrera.getNumEstrellas() + 1);
            holder.tvContadorEstrellas.setText(String.valueOf(carrera.getNumEstrellas()));
            break;
        case R.id.thumb_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_THUMBS)
                    .setValue(carrera.getNumThumbs() + 1);
            holder.tvContadorThumbs.setText(String.valueOf(carrera.getNumThumbs()));
            break;
    }
}

@Override
public void unLiked(LikeButton likeButton) {
    switch (likeButton.getId()){
        case R.id.heart_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_CORAZONES)
                    .setValue(carrera.getNumCorazones() - 1);
            holder.tvContadorCorazones.setText(String.valueOf(carrera.getNumCorazones()));
            break;
        case R.id.star_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_ESTRELLAS)
                    .setValue(carrera.getNumEstrellas() - 1);
            holder.tvContadorEstrellas.setText(String.valueOf(carrera.getNumEstrellas()));
            break;
        case R.id.thumb_button:
            rootDatabaseCarrera
                    .child(carrera.getId())
                    .child(KEY_RUTA_THUMBS)
                    .setValue(carrera.getNumThumbs() - 1);
            holder.tvContadorThumbs.setText(String.valueOf(carrera.getNumThumbs()));
            break;
    }
}




/** ViewHolder del adaptador **/

public class CarrerasViewHolder extends RecyclerView.ViewHolder{

    private ImageView ivFotoPerfil;
    private ImageView ivFotoRecorrido;
    private TextView tvNombreUsuario;
    private LikeButton btnCorazon;
    private LikeButton btnEstrella;
    private LikeButton btnThumb;
    private TextView tvContadorCorazones;
    private TextView tvContadorEstrellas;
    private TextView tvContadorThumbs;

    public CarrerasViewHolder(View itemView) {
        super(itemView);
        // Inflamos los componentes
        ivFotoPerfil = (ImageView) itemView.findViewById(R.id.placeFotoPerfil);
        ivFotoRecorrido = (ImageView) itemView.findViewById(R.id.placeFotoRecorrido);
        tvNombreUsuario = (TextView) itemView.findViewById(R.id.placeNombrePerfil);
        btnCorazon = (LikeButton) itemView.findViewById(R.id.heart_button);
        btnEstrella = (LikeButton) itemView.findViewById(R.id.star_button);
        btnThumb = (LikeButton) itemView.findViewById(R.id.thumb_button);
        tvContadorCorazones = (TextView) itemView.findViewById(R.id.tv_contadorCorazones);
        tvContadorEstrellas = (TextView) itemView.findViewById(R.id.tv_contadorEstrellas);
        tvContadorThumbs = (TextView) itemView.findViewById(R.id.tv_contadorDedos);

        // Ponemos el tipo a los botones
        btnEstrella.setIcon(IconType.Star);
        btnThumb.setIcon(IconType.Thumb);
    }
}

}

我感觉我错过了一些简单的事情,但我没有发现错误。 任何的想法 ? 谢谢!

1 个答案:

答案 0 :(得分:0)

因为它是recyclerview,您的OnLikeListenerOnUnLikeListener是参考。这意味着所有的recyclerview都指向同一个侦听器对象。 替换:

holder.btnCorazon.setOnLikeListener(this);
    holder.btnEstrella.setOnLikeListener(this);
    holder.btnThumb.setOnLikeListener(this);

通过以下方式为每个项目创建一个侦听器对象:

holder.btnCorazon.setOnLikeListener(new View.OnClickListenner ....);