Clear Command Buttons in Message View

Christopher Vollick created

We setup the buttons, but never clear them, so when a person has enough
messages in their view and one of them has a button, as they scroll
around and views get recycled the button can be attached onto any
message and not just the ones that actually specify it.

The documentation makes it a bit unclear if the adapter can be set to
null or not... so I just clear it instead. I could leave it, I suppose,
but that feels a bit weird, and the next time we go through this code
we'll just replace it with another one anyway.

Change summary

src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java | 9 
1 file changed, 9 insertions(+)

Detailed changes

src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java 🔗

@@ -28,6 +28,7 @@ import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
+import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
@@ -909,6 +910,14 @@ public class MessageAdapter extends ArrayAdapter<Message> {
                 viewHolder.commands_list.setOnItemClickListener((p, v, pos, id) -> {
                     ((Conversation) conversation).startCommand(adapter.getItem(pos), activity.xmppConnectionService);
                 });
+            } else {
+                // It's unclear if we can set this to null...
+                ListAdapter adapter = viewHolder.commands_list.getAdapter();
+                if (adapter instanceof ArrayAdapter) {
+                    ((ArrayAdapter<?>) adapter).clear();
+                }
+                viewHolder.commands_list.setVisibility(View.GONE);
+                viewHolder.commands_list.setOnItemClickListener(null);
             }
 
             if (isInValidSession) {