android - Select CheckBox of all ListViewItem -


i got listactivity list items i've created. each item got checkbox in it. in addition, layout got button suppose check check boxes, yet it's not working though onclicklistener of button being called.

this method called list adapter:

    public void changeallcheckstatus(boolean checked)     {         synchronized (items)         {             (itemdata item : items)             {                 item.checked= checked;             }         }         notifydatasetchanged();     } 

and getview method:

@override     public view getview(int position, view convertview, viewgroup parent)     {         view vi = convertview;         if (vi == null)         {             vi = m_inflater.inflate(r.layout.myitem, null);         }         textview nametextview = (textview) vi.findviewbyid(r.id.firsttext);         textview addresstextview = (textview) vi.findviewbyid(r.id.secondtext);         checkbox cb = (checkbox) vi.findviewbyid(r.id.the_checkbox);         nametextview.settext(items.get(position).string1);         addresstextview.settext(items.get(position).string2);         cb.setselected(items.get(position).checked);         return vi;     } 

is there wrong how i'm trying check checkboxes?

 listitem = mylist.getadapter().getview(i, null, null); 

is wrong. shouldn't access listview's items directly. should encapsulate checked status of every single item in dataset, , let getview make work.

assuming have boolean status; member in listview's item, add method in adapter,

public void checkall() {   synchronized(items) {     (item item : items) {        item.status = true;     }   }   notifydatasetchanged(); } 

edit:

it got adapter extends baseadapter receives 2 arrays of strings values. creates view @ specific position setting 2 text boxes corresponding values these 2 arrays.

instead of having 2 arrays, use small class encapsulates information need. e.g

public class item {   public string mfirststring;    public string msecondstring;     public boolean mstatus; }  

and feed baseadapter collection of class. e.g

arraylist<item> mitems = new arraylist<>(); 

getitem return mitem.get(position), , on getview you'll set text , checkbox. e.g.

 item item = getitem(position);  checkbox cb = (checkbox) rootview.findviewbyid(r.id.the_checkbox);  cb.setchecked(item.mstatus); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -