0

I have a RecyclerView and adapter. Now in that adapter, I'm inflating one row. In that row, there are one delete button and one progressbar. So what I'm doing is when user clicks on delete button, I make invisible that delete button, and make visible small progress bar in place of delete button from Adapter class. And also I'm sending position via listener to that attached activity, from that I'm calling AsyncTask.

Now the problem is:

When I got to know via AsyncTask that item is deleted, I again want to make visible delete button and to make invisible progressbar. But this time - from Activity (not from adapter), because I want to do something in activity when I get to know that item is deleted. So I can't implement AsyncTask in adapter.

code:

Adapter

  delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        delete.setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                        listener.onClicked(getAdapterPosition(), eventList.get(getAdapterPosition()).getEventId());
                    }
                }
            });

Activity (in activity I want to visible/invisible adapter row button and p.bar:

 @Override
    public void onDeleteDataReceived(Boolean status, int position) {
        stopShimmerLayout();
        if (status) {
            try {
                eventsList.remove(position);
                mAdapter.notifyItemRemoved(position);
                showToast(context, "Deleted", Toast.LENGTH_SHORT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            showToast(context, "Failed", Toast.LENGTH_SHORT);
        }
    }

See the video for better understanding: https://drive.google.com/open?id=13ZAtnyfGbi2X4JjUTmJsIDy-gt5y51Gr

  • Create a function in adapter and call it from activity. for example mAdapter.yourFunction(); – Tara May 16 '20 at 20:07
  • @Tara But how will I access delete and p.bar. it is inside view holder –  May 16 '20 at 20:10
  • through position you have position parameter in ur activity – Tara May 16 '20 at 20:11
  • @Tara I'm asking that we're accessing holder.delete in bindviewholder like that how will I access delete and p.bar in that function?. NOTE: I created function in adapter not in bindviewholder –  May 16 '20 at 20:14
  • https://stackoverflow.com/questions/32457406/how-to-update-refresh-specific-item-in-recyclerview here is the idea if you don't understand let me know i will create a sample – Tara May 16 '20 at 20:17
  • @Tara Do you think I should make MyViewHolder global and access holder value from bindviewholder ? –  May 16 '20 at 20:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214023/discussion-between-tara-and-shefali-singh). – Tara May 16 '20 at 20:19
  • @Tara Okay, commented –  May 16 '20 at 20:23
  • so what I understood you want to delete an item from the list once you get the response from server? – Gautam May 17 '20 at 07:34
  • @Gautam What is my problem is: Suppose I clicked delete button for first 5 items one by one immediately within a sec. and suddenly I started scrolling recyclerview up and down very fast in that case, it is crashing and also while scrolling time that progressbar should not show in any random items. like this question: https://stackoverflow.com/questions/32493958/why-does-the-input-value-in-edittext-swaps-its-position-while-scrolling-in-a-rec/43112630#43112630 –  May 17 '20 at 13:10
  • @ShefaliSingh to fix the crash you need to check the error in logcat. And regarding your second question, what you meant is if you click delete on 1 or multiple items and scroll, the progress bar takes random positions? – Gautam May 17 '20 at 13:39
  • @Gautam forget about crash. you're right for second case. it swaps the position –  May 17 '20 at 13:45

1 Answers1

0

To fix your problem you can take the below approach.

1) Inside your Eventpojo/model class, declare a boolean isSelectedwhich will be initially false. Now whenever user clicks the row, do `

delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
eventList.get(position).isSelected=true;
delete.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}

So by doing this, we are keeping in memory which object is selected and which is not, but once you recycle your views bindViewHolder will be invoked aagain and UI elements setter will be called again so put a check inside onBindViewHolder()

if(eventList.get(position).isSelected){
    //show progress bar
}else{
   // show delete icon
}

To remove the item, just do the following changes in your adapter-

public void removeItem(int position){
    eventList.remove(position)
    notifyItemRemoved(position)
}
Gautam
  • 2,772
  • 3
  • 20
  • 30
  • This I fixed via some different way. But I told you no the position swap problem, that is not solved –  May 17 '20 at 13:52
  • See: https://drive.google.com/open?id=1ePOfZctEO_IhzUM3bFYW3VJKThoVkI6a –  May 17 '20 at 13:56
  • @ShefaliSingh why you want to do this if you are anyhow going to remove the item? why do you want to change the visibility? since the item is anyhow going to be removed. Just remove the object from the list i.e list.removeAt(position) & do adapter.notifyitemremoved(position) – Gautam May 17 '20 at 13:57
  • I'm not that much bother about that. The main thing is while it's deleting and progressbar running, if I'm scrolling the recyclerview, that running progressbar is swaping into some random position –  May 17 '20 at 14:01
  • That's what, since you are going to remove just do two operations as mentioned above. Now regarding its swapping the position is only because of views being recycled and you might not be strong the boolean as suggested above. Have you made the changes as suggested above? Also is your item getting removed from list or not? – Gautam May 17 '20 at 14:02
  • Okay, let me do this also –  May 17 '20 at 14:06
  • Sure, also do check if the correct item is getting removed or not from the list. This will help you in debugging the issue furthermore – Gautam May 17 '20 at 14:08
  • I tried, but the same problem persists .Check updated code: https://drive.google.com/open?id=1ePOfZctEO_IhzUM3bFYW3VJKThoVkI6a –  May 17 '20 at 14:55
  • correct items is being removed, that was working previously also, but while scrolling progressbar swaps pos randomly –  May 17 '20 at 14:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214056/discussion-between-gautam-and-shefali-singh). – Gautam May 17 '20 at 15:00