android - How to get values of all EditText inside a ListView -
i want values of edittext elements present inside listview. code :
final listview editorlist = (listview) findviewbyid(r.id.editorlist); final editoradapter adapter = new editoradapter(context, data); editorlist.setadapter(adapter); button commitbutton = (button) findviewbyid(r.id.commit_button); commitbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub try{ //system.out.println("size of list : " + editorlist.getchildcount()); for(int =0;i< data.size() ;i++){ system.out.println("size of list : " + data.size()); edittext value = adapter.getitem(i); string propertyvalue = value.gettext().tostring(); system.out.println("propertyvalue : " + propertyvalue); } } catch (exception e){ e.printstacktrace(); } } });
this adapter class :
package in.omerjerk.preferenceseditor; public class editoradapter extends baseadapter { context context; arraylist<hashmap<string,string>> data; edittext[] mholders; public editoradapter(context context, arraylist<hashmap<string,string>> data){ this.context = context; this.data = data; system.out.println("no . of items in nodes"+data.size()); mholders = new edittext[data.size()]; } @override public int getcount() { // todo auto-generated method stub return data.size(); } @override public edittext getitem(int pos) { // todo auto-generated method stub return mholders[pos]; } @override public long getitemid(int pos) { // todo auto-generated method stub return pos; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub viewholder holder; if(convertview==null){ system.out.println("convert view null"); holder = new viewholder(); convertview = layoutinflater.from(context).inflate(r.layout.row_edit_string,null,false); holder.editpropertyvalue = (edittext) convertview.findviewbyid(r.id.propertyvalue); holder.propertyname = (textview) convertview.findviewbyid(r.id.propertyname); holder.propertyname.settext(data.get(position).get("propertyname")); holder.editpropertyvalue.settext(data.get(position).get("propertyname")); convertview.settag(holder); }else{ system.out.println("convert view not null"); holder = (viewholder) convertview.gettag(); holder.propertyname.settext(data.get(position).get("propertyname")); holder.editpropertyvalue.settext(data.get(position).get("propertyname")); convertview.settag(holder); mholders[position] = new edittext(context); mholders[position] = holder.editpropertyvalue; } return convertview; } }
i'm getting strange error in output. mholders array contain upto 6-7 elements , these elements repeated in entire array. i'm able values of edittext error not correct.
this not going work way you'd expect. views recycled when using adapter
. means there many inflated views there visible on screen (plus couple). so, if you're trying iterate on of children, find offscreen items return null.
the proper way use collection
of objects represent values of edittext
adapter
data. way, in getview
check value of object @ position, , call settext()
on view. when want values, create method getitems()
in adapter
, , iterate on collection
.
if post relevant parts of adapter
code, can show how this.
Comments
Post a Comment