0
I have a simple recyclerView and Adapter below where I do the CRUD before saving in BD, and everything works except the update, where I update the item by Adapter but the view in the list does not update the data. If you click the same item to edit it again it shows the values that have already been updated and not shown in the list.
Down with my Adapter:
public class InsumoAdapter extends RecyclerView.Adapter<InsumoAdapter.InsumoViewHolder>{
private final Context context;
private List<Insumo> insumos;
public InsumoAdapter(Context context, List<Insumo> insumos) {
this.context = context;
this.insumos = insumos;
}
@Override
public InsumoAdapter.InsumoViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.card_teste, viewGroup, false);
InsumoAdapter.InsumoViewHolder holder = new InsumoAdapter.InsumoViewHolder(view);
return holder;
}
public static class InsumoViewHolder extends RecyclerView.ViewHolder{
public TextView tNome, tVlrCusto;
ImageButton btnDelete;
CardView cardView;
public InsumoViewHolder(View view){
super(view);
tNome = (TextView) view.findViewById(R.id.txtNomeProd);
tVlrCusto = (TextView) view.findViewById(R.id.txtValorProd);
btnDelete = (ImageButton) view.findViewById(R.id.imgBtnDel);
cardView = (CardView) view.findViewById(R.id.cardProduto);
}
}
@Override
public void onBindViewHolder(final InsumoViewHolder holder, final int position) {
if(insumos.size() != 0){
final Insumo p = insumos.get(position);
holder.tNome.setText(p.getNomeInsumo());
holder.tVlrCusto.setText(p.getValor().toString());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicou no Card para Editar na posição "+position,Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
//bundle.putString("nome", p.getNomeInsumo());
//bundle.putDouble("valor", p.getValor());
bundle.putInt("position", position);
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtras(bundle);
context.startActivity(intent);
//update(p, position);
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicou no Card para Excluir na posição "+position,Toast.LENGTH_SHORT).show();
delete(position);
}
});
}
}
@Override
public int getItemCount() {
return this.insumos != null ? this.insumos.size() : 0;
}
private void excluirInsumo(Context context, final int i){
String titulo = "Excluir";
String msg = "Deseja excluir este item?";
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(titulo);
alertDialog.setMessage(msg);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Sim",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//deleta("Pproduto",produtos.get(i).get_id());
AddActivity addActivity = new AddActivity();
//addActivity.deleta(i);
dialog.dismiss();
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Não",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
private void delete(int position){
insumos.remove(position);
notifyItemRemoved(position);
}
public void update(Insumo insumo, int position){
insumos.set(position, insumo);
//notifyItemChanged(position);
notifyDataSetChanged();
}
public void insere(Insumo insumo){
insumos.add(insumo);
notifyDataSetChanged();
}
It’s as if I haven’t come back to onBindViewHolder to update the data. Could someone give me a light? I know it must be something silly, but I can’t find it at all.
notifyItemChanged(position);
is not going?– Murillo Comino
pq
//update(p, position);
on onClick is commented?– Murillo Comino
I’m sorry Murilo, notifyItemChanged(position); it didn’t work and the update is commented because I call Activity and from there I call the update.
– Cabral