naresh
naresh

Reputation: 10392

android - How to uncheck the already selected checkedtextview in sigle choice listview

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);            
                    }
                }
            }); 

Upvotes: 2

Views: 1833

Answers (2)

Nas
Nas

Reputation: 2198

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();

                }
            });

Upvotes: 1

sstn
sstn

Reputation: 3069

Try lstAttribs.clearChoices().

Upvotes: 0

Related Questions