checkbox - Adding checked items in a custom list view to array list in android -
hi writing program using custom listview checkbox. intention start new activity when clicking button (add) , new activity displays selected values listview.i getting error when try add values
the error occuring line is
selecteditems.add(adapter.getitem(position));
if comment line can check box shows eror when click "add" button.
here complete code.
my mainactivity
homepage.java
public class homepage extends activity { private listview listview1; listadapter adapter; button btn; sparsebooleanarray checkedvalue; arraylist<list> selecteditems; string name; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_page); list device_data[] = new list[] { new list(r.drawable.ic_pic , "actvity1"), new list(r.drawable.ic_pic, "actvity2"), new list(r.drawable.ic_pic, "actvity3"), new list(r.drawable.ic_pic, "actvity4"), new list(r.drawable.ic_pic, "actvity5") }; listadapter adapter = new listadapter(this, r.layout.list_viewrow, device_data); listview1 = (listview)findviewbyid(r.id.listview1); view header = (view)getlayoutinflater().inflate(r.layout.listview_header, null); listview1.addheaderview(header); listview1.setadapter(adapter); log.i("check box status", ""+listadapter.listholder.class); btn= (button)findviewbyid(r.id.add); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string[] outputstrarr = new string[selecteditems.size()]; checkedvalue= listview1.getcheckeditempositions(); (int = 0; < selecteditems.size(); i++) { outputstrarr[i] = selecteditems.get(i).tostring(); } intent intent = new intent(getapplicationcontext(), checkedvalues.class); bundle b = new bundle(); b.putstringarray("selecteditems", outputstrarr); // add bundle intent. intent.putextras(b); startactivity(intent); } }); } private class listadapter extends arrayadapter<list> { context context; int layoutresourceid; boolean checkvalue; list data[] = null; public listadapter(context context, int layoutresourceid, list[] data) { super(context, layoutresourceid, data); this.layoutresourceid = layoutresourceid; this.context = context; this.data = data; } @override public view getview(final int position, view convertview, viewgroup parent) { view row = convertview; final listholder holder; if(row == null) { layoutinflater inflater = ((activity)context).getlayoutinflater(); row = inflater.inflate(layoutresourceid, parent, false); holder = new listholder(); holder.imgicon = (imageview)row.findviewbyid(r.id.imgicon); holder.txttitle = (textview)row.findviewbyid(r.id.txttitle); holder.checkbox=(checkbox) row.findviewbyid(r.id.check); holder.checkbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { selecteditems.add(adapter.getitem(position)); } }); row.settag(holder); } else { holder = (listholder)row.gettag(); } list list = data[position]; holder.txttitle.settext(list.title); holder.imgicon.setimageresource(list.icon); return row; } class listholder { imageview imgicon; textview txttitle; checkbox checkbox; } } }
and second activity
checkedvalues.java
public class checkedvalues extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_checked_values); bundle b = getintent().getextras(); string[] resultarr = b.getstringarray("selecteditems"); listview lv = (listview) findviewbyid(r.id.outputlist); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, resultarr); lv.setadapter(adapter); }
}
thanks in advance..
initialize "arraylist"
arraylist<list> selecteditems; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home_page); selecteditems = new arraylist<list>(); }
Comments
Post a Comment