android - EditText in ListView loses content on scroll in ListView -
i'm working on app users fill reports , polls. in activity have listview children contain edittexts, checkbox , textview.
the problem edittexts , checkbox when scrolling, content have lost. able fix checkbox problem saving check status in array, can't fix problem edittexts. know there questions particular issue solutions provided doesn't seem work. here adapter:
public class diveprofileadapter extends arrayadapter<diveprofile> { private context mcontext; private list<diveprofile> mtasks; viewholder holder; private arraylist<boolean> itemchecked = new arraylist<>(); //for saving checkbox values public diveprofileadapter(context context, list<diveprofile> objects) { super(context, r.layout.row_dive_profile_divers_list_item, objects); this.mcontext = context; this.mtasks = objects; (int = 0; < this.getcount(); i++) { itemchecked.add(i, false); // initializes items value false } } public view getview(final int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater mlayoutinflater = layoutinflater.from(mcontext); convertview = mlayoutinflater.inflate(r.layout.row_dive_profile_divers_list_item, null); holder = new viewholder(); holder.diverattended = (checkbox) convertview.findviewbyid(r.id.diver_attended_checkbox); holder.divetime = (edittext) convertview.findviewbyid(r.id.dive_time_edit_text); holder.depth = (edittext) convertview.findviewbyid(r.id.depth_edit_text); convertview.settag(holder); }else { holder = (viewholder) convertview.gettag(); } final diveprofile diverobjecttask = mtasks.get(position); holder.diverattended.setchecked(diverobjecttask.isdiverattended()); holder.divetime.settext(diverobjecttask.getdivetime()); holder.diverattended.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { checkbox cb = (checkbox) v.findviewbyid(r.id.diver_attended_checkbox); if (cb.ischecked()) { itemchecked.set(position, true); // operations here } else if (!cb.ischecked()) { itemchecked.set(position, false); // operations here } } }); holder.diverattended.setchecked(itemchecked.get(position)); // check or uncheck holder.divetime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string txt = setdivetimedialog(); holder.divetime.settext(txt); mtasks.get(position).setcurrenttext(txt); //add setter current item's position } }); holder.divetime.settext(diverobjecttask.getcurrenttext()); return convertview; } static class viewholder { checkbox diverattended; edittext divetime; edittext depth; } private string setdivetimedialog() { //final string[] items = finitems; final string[] items = getcontext().getresources().getstringarray(r.array.certification_level_items); // create list dialog required parameters - context, title, , our array of items fill list. customlistdialog.builder builder = new customlistdialog.builder(getcontext(), "dive time", items); // again can use methods on builder customise more. builder.titlealignment(basedialog.alignment.left); builder.itemalignment(basedialog.alignment.left); builder.titlecolor(getcontext().getresources().getcolor(r.color.primary_color)); // can build our dialog. customlistdialog customlistdialog = builder.build(); // can show it. customlistdialog.show(); customlistdialog.setlistclicklistener(new customlistdialog.listclicklistener() { @override public void onlistitemselected(int i, string[] strings, string s) { result = s; } }); return result; } }
you should :
final diveprofile diverobjecttask = mtasks.get(position); holder.divetime.settext(diverobjecttask.getdivetie()); //also set checkbox value
those values become empty because dont set in getview
(getview
automatically getting called when list scrolled)
update
looking @ problem, think easiest way create new variables in diveprofile
class, such boolean isselected
, string currenttext
.
after that, theres no need use separate list itemchecked
. can get
, set
current item's value.
example using getter
:
holder.diverattended.setchecked(mtasks.get(position).getselected()); // getselected isselected getter method
and call setselected
(setter
of isselected
) in holder.diverattended
onclick
example edittext
setter
:
holder.divetime.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string txt = setdivetimedialog(); holder.divetime.settext(txt); mtasks.get(position).setcurrenttext(txt); //add setter current item's position } });
and call getcurrenttext()
in getview
Comments
Post a Comment