Wolf87
Wolf87

Reputation: 540

deselect list view item?

I have list view with names and numbers and checkbox populated from contacts with SimpleAdapter, and on item click I listen event and change color of item, also I want to change checkbox to enabled and after user click one more time on the same item I want to deselect that item. How can I do that? Thanks Wolf.

This is my code, I can successfully change color, but I don't now how to change checkbox state for that specific item and than on second click to change that state again:

lImenik.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
         // TODO Auto-generated method stub
         Object obj = lImenik.getItemAtPosition(position);
         String tmp = obj.toString();
         view.setBackgroundColor(Color.GRAY);

         for(int i = 0; i < adapter.getChildCount(); i++){
         if(i == position){
         if(position != prePos){

             adapter.getChildAt(i).setBackgroundColor(Color.GRAY);                              
             prePos = position;                             
          } else {
             adapter.getChildAt(i).setBackgroundColor(0x191919);
             prePos = -1;
          }                         
        }                       
      }                 
     }
  });
}

Upvotes: 2

Views: 2514

Answers (1)

Arslan Anwar
Arslan Anwar

Reputation: 18746

The method

public void onItemClick(AdapterView<?> adapter, View view,
                    int position, long id)

provides you the view of row which you have clicked. All what you have to do is to mark or unmark the checkbox in that view. You can do that by

public void onItemClick(AdapterView<?> adapter, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Object obj = lImenik.getItemAtPosition(position);
            CheckBox checkbox = ( CheckBox ) view.findViewById ( R.id.your_checkedbox_id );
            checkbox.setChecked ( !checkbox.isChecked() ) ;

But you should have array , or a boolean value for your objects so that at the end you can know that Which object are marked are check and which are not

Upvotes: 2

Related Questions