0

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);

    }    
}
TofferJ
  • 4,678
  • 1
  • 37
  • 49

5 Answers5

2

RecyclerView recycles views that are not visible to the user anymore. That means, that the one you have made INVISIBLE will be invisible when it's recycled. For each item you have to restore the state of the view by setting holder.b1.setVisibility(View.VISIBLE); in onBindViewHolder. However, it will reset those items that are sent to the server side. You have to implement logic to save a state of the items. Let's say listitem.isSent(). Then you will have:

final Ardlist_item listitem = listitems.get(position);

holder.b1.setVisibility(listitem.isSent()? View.INVISIBLE : View.VISIBLE);
holder.textitemname.setText(listitem.getItemname());
...
            holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listitems.setSent(true);

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

            }
        });
MeliX
  • 230
  • 1
  • 6
  • isSent() and setSent(true); is not defind where did i define –  Jul 25 '17 at 05:22
  • The problem is that When submit the button(data will send and button visiblity set to INvisble) and scroll down and up the submited list is remain same ie visibility changes to visble . please help –  Aug 17 '17 at 10:02
1

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;//chnaged
   private boolean active = true;

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Fori
  • 475
  • 5
  • 15
  • add attr "active.. can u explain this . –  Jul 25 '17 at 06:03
  • The problem is that When submit the button(data will send and button visiblity set to INvisble) and scroll down and up the submited list is remain same ie visibility changes to visble . please help –  Aug 17 '17 at 10:02
  • The code above should work. You need call setVisibility just inside onBindViewHolder not only inside onClickListener. – Fori Aug 17 '17 at 10:13
  • yes am using this code but still the issue. please help me. –  Aug 17 '17 at 10:16
  • i add in onbind holder.b1.setVisibility(listitem.isActive() ? View.VISIBLE : View.INVISIBLE); –  Aug 17 '17 at 10:18
  • please help me https://drive.google.com/file/d/0B00vUl6c-btDQlhQVEpIbW9jVVk/view?usp=sharing –  Aug 17 '17 at 10:23
  • You are calling `listitem.setActive(true);` before changing visibility. Remove this line. – Fori Aug 17 '17 at 10:39
  • Inside class Ardlist_item set default active to true. I updated answer: `private boolean active = true;` – Fori Aug 17 '17 at 10:50
  • Like to help, so please confirm my answer. – Fori Aug 17 '17 at 11:03
  • sir, i need your help, when i change the edit text value and scroll down and up the edit text value changes to first value, please help me sir –  Aug 21 '17 at 07:18
  • Just save edittext value after its change to listitem. And in onbind update edittext. – Fori Aug 21 '17 at 14:16
  • I tried sir, i coudnt can you explain it will help me alot sir –  Aug 21 '17 at 15:06
  • I tried using shared preferences but I couldn't achieve please help me –  Aug 22 '17 at 08:16
  • I tried using shared preferences but I couldn't achieve please help me –  Aug 22 '17 at 08:17
  • Dont use shared preferences, here is example: https://stackoverflow.com/a/31860393/5240077 – Fori Aug 22 '17 at 08:30
  • sir , when i tried i got this error java.lang.NullPointerException: Attempt to read from null array at com.nic.fps.ArdAdapter.onBindViewHolder(ArdAdapter.java:118) at com.nic.fps.ArdAdapter.onBindViewHolder(ArdAdapter.java:55) –  Aug 26 '17 at 07:29
  • https://drive.google.com/file/d/0B00vUl6c-btDZVNhVlpudHVOcnc/view?usp=sharing need your help sir –  Aug 26 '17 at 07:35
1

even I was facing the same issue , there are 2 ways to solve this problem ;

  1. {your recycler view}.setItemViewCacheSize(9); I found 9 to work perfectly with my code and probably should work with even yours.

  2. In Your Adapter Class , under onBindViewHolder Add A line " holder.bind(userModal1); "

    then create a method named bind under the ViewHolder

    then set the same textview , imageview , button same as you did in the onBindViewHolder

for a better understanding i have added a photo as well

first method to solve the issue

{

cache one

}

this is the second method

{

onBindViewHolder

ViewHolder---method bind

}

Daksh Rawal
  • 238
  • 2
  • 13
0

simply add this line holder.b1.setVisibility(View.VISIBLE); after holder.rcqty.setText(listitem.getQty()); it will do. It happens because the view is recycled and on recycling it is found that its visibility is gone which arises the problem that you are facing.

Ashwani
  • 1,294
  • 1
  • 11
  • 24
  • when list is refreshed or ... button visibility automatically will set to visible . –  Jul 25 '17 at 05:25
  • The problem is that When submit the button(data will send and button visiblity set to INvisble) and scroll down and up the submited list is remain same ie visibility changes to visble . please help –  Aug 17 '17 at 10:02
  • Not able to understand what you are trying to say..update your question – Ashwani Aug 17 '17 at 13:04
  • gald to heard that..:) – Ashwani Aug 18 '17 at 05:53
0

I was facing same problem. Hopefully, after a lot of research, I found a solution.

Actually recyclerview creates 8 view holders after then it reuses the already made adaptors. So if you make any change in them it will be continue to every 7th view holder cause both are same.

So to solve it you have to initialise all views of viewholders everytime you use.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51