Search code examples
androidlistviewnotifydatasetchanged

Android - ListView notifyDataSetChanged is calling ACTION_CANCEL on button presses, can I avoid this?


I'll start out with the scenario, it's a tricky one: I have a ListView with a single button on each row. These buttons have the ability to be held down by the user. There is a chance that from a background thread that something will happen to call notifyDataSetChanged() on the ListView. This will cause ACTION_CANCEL to be called on any of the button presses, interrupting the users interaction with holding down the button.

I've tried a couple things, but I think I've been missing the main cause of my problem which is that on a notifyDataSetChanged() ACTION_CANCEL is called on any of it's children.

I'm currently using a BaseAdapter. I need to update a TextView that is next to the button whenever the background thread calls notifyDataSetChanged(), but I can't let it interrupt the held button. I'm currently trying to manually just update the text field, but it's not redrawing even when calling invalidate.

I'll post my solution if I find one.


Solution

  • Figured it out! This is what I went with. Instead of calling notifyDataSetChanged() I did this.

        int start = list.getFirstVisiblePosition();
        int end = list.getLastVisiblePosition();
    
        for (int i = start; i <= end; i++) {
            View view = list.getChildAt(i);
            if (view != null) {
                TextView textNotification = (TextView) view.findViewById(R.id.text_notification);
                textNotification.setText("place your text here");
            }
        }
    

    I found the solution here: Redraw a single row in a listview. Looks like that solution worked for me as well! I only call this new code if one of the buttons is held, else I just use notifyDataSetChanged().