android - Whats the best way to do a loop for listview selections -
i have listview populated list of checkboxes..i want add button when press it sth each item user selected in listview..whats best way find check box checked?i know can use setoncheckedchangelistener
how call if each checkbox in list?and if create array string , each time checkbox checked addes text array list can loop each selected item? here code pupulate list view
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_database_list); file path=new file(clubcp.sdcardpath); list<string> file_lists = main.directorypath(path); listview database_list=(listview)findviewbyid(r.id.database_list); arrayadapter<string> adapter = new arrayadapter<string>(this, r.layout.database_list_item,r.id.chk_database_list_item,file_lists); arrayadapter<string> adapter1 = new interactivearrayadapter(this,file_lists); database_list.setadapter(adapter1); }
i searched , think must use arrayadapter ..i writing class..this class package co.tosca.persianpoem;
import java.util.list; import android.app.activity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.checkbox; import android.widget.compoundbutton; public class interactivearrayadapter extends arrayadapter<string> { private final list<string> list; private final activity context; public interactivearrayadapter(activity context, list<string> list) { super(context, r.layout.database_list_item, list); this.context = context; this.list = list; } static class viewholder { protected checkbox checkbox; } @override public view getview(int position, view convertview, viewgroup parent) { view view = null; if (convertview == null) { layoutinflater inflator = context.getlayoutinflater(); view = inflator.inflate(r.layout.database_list_item, null); final viewholder viewholder = new viewholder(); viewholder.checkbox = (checkbox) view.findviewbyid(r.id.chk_database_list_item); viewholder.checkbox .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { ///do sth } }); view.settag(viewholder); viewholder.checkbox.settag(list.get(position)); } else { view = convertview; ((viewholder) view.gettag()).checkbox.settag(list.get(position)); } return view; } }
thanks help
you need subclass arrayadapter , override getview
method can call setonclicklistener
on per-view basis. see arrayadapter docs: here
public view getview (int position, view convertview, viewgroup parent) { view itemview = convertview; if (itemview == null) { layoutinflater li = layoutinflater.from(getcontext()); itemview = li.inflate(r.layout.database_list_item); } checkbox cb = (checkbox)itemview.findviewbyid(r.id.checkbox); // whatever id of checkbox in layout cb.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox mycb = (checkbox)v; if (mycb.ischecked()) { // add position array representing selected items } else { // remove position array } } }); return itemview; }
Comments
Post a Comment