android - Custom Listview - multiselect items and colour -


i using custom listview adapter, , working well.

i using checkbox stored checked values, think might better change background colour of list item when selected.

i have tried (and failed) using selectors, , tried modify code change view background.

here custom adapter code, can change set blue background when row selected, , maintain selection if multiple selected?

public class imageadapter extends baseadapter {     context context;     arraylist<bitmap> images;     arraylist foldername;     boolean[] checkboxstate;      public imageadapter(context c, arraylist<bitmap> images, arraylist foldername){          this.context = c;         this.images = images;         this.foldername = foldername;         this.context = context;         checkboxstate=new boolean[images.size()];      }      public int getcount() {         return images.size();     }       public object getitem(int arg0) {         // todo auto-generated method stub         return null;     }       public long getitemid(int arg0) {         // todo auto-generated method stub         return 0;     }      private class viewholder {         textview title;         imageview iconimage;         checkbox checkbox;     }       public void performclick(integer position) {          if(ischecked(position))         {             checkboxstate[position] = false;         }         else         {             checkboxstate[position] = true;          }          notifydatasetchanged();     }      public void selectedall(arraylist<integer> checkedpositions) {         for(int = 0; i< checkedpositions.size(); i++){             checkboxstate[checkedpositions.get(i)] = true;         }          notifydatasetchanged();     }      public void deselectedall(arraylist<integer> checkedpositions) {         for(int = 0; i< checkedpositions.size(); i++){             checkboxstate[checkedpositions.get(i)] = false;         }          notifydatasetchanged();     }      public boolean ischecked(integer pos){         if(checkboxstate[pos] == true)     return true;         else             return false;     }       public view getview(final int position, view arg1, viewgroup arg2) {         layoutinflater inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);          //view gridview;         view v = arg1;         viewholder holder;          if (arg1 == null) {             layoutinflater vi = (layoutinflater) context                     .getsystemservice(context.layout_inflater_service);             v = vi.inflate(r.layout.list_row, null);              holder = new viewholder();             holder.title = (textview) v.findviewbyid(r.id.filename);              holder.iconimage = (imageview) v.findviewbyid(r.id.list_image);             holder.checkbox = (checkbox)v.findviewbyid(r.id.checkbox1);              v.settag(holder);          } else {             holder = (viewholder) v.gettag();         }              holder.title.settext(foldername.get(position).tostring());           holder.iconimage.setimagebitmap(images.get(position));          //set state of         //checkbox using boolean array         holder.checkbox.setchecked(checkboxstate[position]);           //for managing state of boolean         //array according state of         //checkbox          holder.checkbox.setonclicklistener(new view.onclicklistener() {              public void onclick(view v) {                 if(((checkbox)v).ischecked())                 {                     checkboxstate[position]=true;                  }                 else                 {                     checkboxstate[position]=false;                 }              }          });              return v;          }  } 

looks have state of checkboxes working multiple selection, great!

why not use method background colour too?

find line in code (your adapter)

    //set state of     //checkbox using boolean array     holder.checkbox.setchecked(checkboxstate[position]); 

and put underneath

v.setbackgroundcolor( (checkboxstate[position]) ? color.parsecolor("#0000ff") : color.parsecolor("#000000") ); 

change colours match needs first colour selected colour wish, whereas second colour colour when item not selected.

enjoy!


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -