mercredi 26 octobre 2016

Calling setItemChecked() from inside of an itemClickListener doesn't work

I am trying to set the item checked when the user long clicks on it (like on Whatsapp and most of the apps). The normal click works when selecting other items, including the one that started the CAB, but I can't get the initial item to get checked.

This is my code (inside of a custom adapter, tried it from the activity with same results)

vi.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("CL", "State called");

                if(((MainActivity) mContext).ListSelectionInProgress)
                {
                    if(!((MainActivity) mContext).list.isItemChecked(position))
                    {
                        ((MainActivity) mContext).list.setItemChecked(position, true);
                    }
                   else
                    {
                       ((MainActivity) mContext).list.setItemChecked(position, false);
                    }

                    Log.d("State", Integer.toString(((MainActivity) mContext).list.getCheckedItemCount()));
                }
            }

        });

        vi.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if(!((MainActivity) mContext).list.isItemChecked(position))
                {
                    ((MainActivity) mContext).list.setItemChecked(position, true);
                }
                else
                {
                    ((MainActivity) mContext).list.setItemChecked(position, false);
                }

                if(!((MainActivity) mContext).ListSelectionInProgress)
                {
                    ((MainActivity) mContext).showActionMode();
                }

                return false;
            }
        });

I'm not sure if I explained it correctly, so here are the steps of what is going on.

What I want to achieve

1) User long clicks a ListView item

2) Item gets checked (or highlighted in my case)

3) User then can click on other items to check and uncheck them, including the item from number 2

What is actually going on

1) User long clicks on the ListView item

2) Item is still the same (not checked, although list.getCheckedCount() returns 1)

3) User needs to click on the item again to properly check/highlight it

4) User can then click on other items to check/uncheck them

What am I doing wrong here exactly? This listview checkking made me pull my hair out, at first I thought I had to use setSelected(). After that I realized it's just for setting the selection but recycled views don't work with it (gets destroyed). Then I realized I needed to use checkboxes (or a similar approach) which I managed to do, but it's just way too complicated for a simple app functionality that is a standard today. Maybe it's because I'm still a hobby developer, but still it seems overly complicated.

I would appreciate any help or pointers on this one, thank you :)




Aucun commentaire:

Enregistrer un commentaire