Update Recyclerview does not update the item in the list

Asked

Viewed 225 times

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?

  • pq //update(p, position); on onClick is commented?

  • 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.

1 answer

1


To keep it as a record to help those who are also having the same problem, mine was that I was not with the same instance when I called the update. The solution was to create a class that, in the Activity of my Recycler after instantiating Adapter I give a set and then in the Activity of update I give a get, so that I always have the same estancia of Adapter. class that receives and makes available the Adapter instance:

 private static InsumoAdapter insumoAdapter;

    public static InsumoAdapter getMainAdapter(){
        return insumoAdapter;
    }

    public static void setMainAdapter(InsumoAdapter adapter) {
        insumoAdapter = adapter;
    }

setting the time

 mInsumoAdapter = new InsumoAdapter(getBaseContext(), insumos);
        //setando o adapter na classe
        setMainAdapter(mInsumoAdapter);
        recyclerView.setAdapter(mInsumoAdapter);
        mInsumoAdapter.notifyDataSetChanged();

taking the instance for the update

insumoAdapter = getMainAdapter();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.