Avinash Avinash - 1 year ago 51
Android Question

Button click on recycler view is repeating

I have a

RecyclerView
. Data comes from a server. A list item view contains:


  • one
    TextView

  • one
    EditText

  • one
    Button



I have 10 list items. When I click the button data will send to the server, and after successful submission the button visibility is set to View.INVISIBLE. The problem is that every 7th button is also changed to invisible. The data transfer is working properly. Please help me.

Bind the view holder

@Override
public void onBindViewHolder(final ViewHolder1 holder, final int position) {
final Ardlist_item listitem = listitems.get(position);

holder.textitemname.setText(listitem.getItemname());
holder.liftqty.setText(listitem.getQty());

holder.rcqty.setText(listitem.getQty());

holder.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

holder.b1.setVisibility(View.INVISIBLE);
holder.rcqty.setEnabled(false);
Toast.makeText(context, "clicled" + position, Toast.LENGTH_LONG).show();

}
});
}


Getting the position

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

public class ViewHolder1 extends RecyclerView.ViewHolder {

public TextView liftqty;

public Button b1;
public EditText rcqty;
public ViewHolder1(View itemView) {

super(itemView);
textitemname = (TextView) itemView.findViewById(R.id.item_name);
liftqty = (TextView) itemView.findViewById(R.id.lifted_qty);

b1 = (Button) itemView.findViewById(R.id.receive_btn);
rcqty = (EditText) itemView.findViewById(R.id.received_qty);

}
}

Answer Source

You need update every view inside onBindViewHolder. So add attr "active" to your model and update after click:

    @Override
    public void onBindViewHolder(final ViewHolder1 holder, final int position) {
        final Ardlist_item listitem = listitems.get(position);

        holder.textitemname.setText(listitem.getItemname());
        holder.liftqty.setText(listitem.getQty());
        holder.rcqty.setText(listitem.getQty());

        holder.b1.setVisibility(listitem.isActive() ? View.VISIBLE : View.INVISIBLE); // set visibility
        holder.rcqty.setEnabled(listitem.isActive());

        holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Ardlist_item listitem = listitems.get(position);
                listitem.setActive(false); // edit active attribute
                holder.b1.setVisibility(View.INVISIBLE);
                holder.rcqty.setEnabled(false);
                Toast.makeText(context, "clicled" + position, Toast.LENGTH_LONG).show();
            }
        });
    }

Add attr active:

class Ardlist_item {
    private boolean active = true;

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download