2

I am trying to uncheck the already selected checkedtextview in single choice list.I tried with the following code but it's not working. Please can anyone help me. How do we know, we are trying to select the already selected one pro grammatically?

Code

lstAttribs.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView arg0, View v, int nItemPosition,long arg3)
                {    
                    if(lstAttribs.isItemChecked(nItemPosition))                     
                    {   

                         lstAttribs.setItemChecked(nItemPosition, true); 

                    } 
                    else 
                    {
                        lstAttribs.setItemChecked(nItemPosition, false);            
                    }
                }
            }); 
naresh
  • 10,332
  • 25
  • 81
  • 124
  • see this http://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-in-android – Senthil Feb 14 '13 at 05:59

2 Answers2

1

Its too late but may help some one.

set your listview choice mode to single choice and declare global integer variable with value -1.

int cPos=-1;

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    if (cPos == position) {
                        if(listView.isItemChecked(cPos)){
                            listView.setItemChecked(position, false);   
                        }else{
                            listView.setItemChecked(position, true);
                        }
                    } else {
                        listView.setItemChecked(position, true);
                    }
                    cPos = listView.getCheckedItemPosition();

                }
            });
Nas
  • 2,158
  • 1
  • 21
  • 38
0

Try lstAttribs.clearChoices().

sstn
  • 3,050
  • 19
  • 32