android - addTextChangedListener is not working inside Base Adapter -
i have implemented customized listview name,price,quantity etc,
by default quantity 1 in list items.
if user changes 1 of list item's quantity 1 2 , changed quantity has updated in sqlite database.
problem
i tried using addtextchangedlistener
updated data user input , update in sqlite database well. addtextchangedlistener
not working .
i unable update required field in database.
below code use addtextchangedlistener
.
@override public view getview(final int position, view convertview, final viewgroup parent) { view rowview = convertview; if (convertview == null) { // todo auto-generated method stub layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); rowview = inflater.inflate(r.layout.list_items_cart, null); holder.tv_name = (textview) rowview.findviewbyid(r.id.name_cart); holder.tv_price = (textview) rowview.findviewbyid(r.id.price_cart); holder.image = (imageview) rowview.findviewbyid(r.id.image_cart); holder.tv_model = (textview) rowview.findviewbyid(r.id.model_cart); holder.tv_product = (textview) rowview.findviewbyid(r.id.product_cart); holder.delete = (button) rowview.findviewbyid(r.id.delete); holder.quantity = (edittext) rowview.findviewbyid(r.id.quantity); rowview.settag(holder); } else holder = (holder) rowview.gettag(); holder.tv_name.settext(list_name.get(position)); name = holder.tv_name.gettext().tostring(); holder.tv_price.settext(list_price.get(position)); price = holder.tv_price.gettext().tostring(); holder.tv_model.settext(list_model.get(position)); model = holder.tv_model.gettext().tostring(); holder.tv_product.settext(list_productid.get(position)); product = holder.tv_product.gettext().tostring(); holder.quantity.settext(quant_items.get(position)); quant = holder.quantity.gettext().tostring(); holder.image.setimagebitmap(loadimagefromstorage(list_images.get(position))); image_new = holder.image.tostring(); holder.quantity.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void aftertextchanged(editable s) { value = s.tostring().trim(); quant.replace(holder.quantity.gettext().tostring(), value); updatetable(); } }); final holder finalholder = holder; holder.delete.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { databasehandler db = new databasehandler(context); //sqlitedatabase db1 = db.getwritabledatabase(); // db.onupgrade(db1,0,1); deleteuser(finalholder.tv_model.gettext().tostring()); listview list = (listview)parent; cart_refresh.notifydatasetchanged(); list.setadapter(cart_refresh); // db.deletecontact(new cart(holder.tv_name.gettext().tostring(),holder.tv_price.gettext().tostring() // ,holder.image.tostring(),holder.tv_model.gettext().tostring())); } }); final view finalrowview = rowview; rowview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string name_item = ((textview) finalrowview.findviewbyid(r.id.name_cart)).gettext().tostring(); string price_item = ((textview) finalrowview.findviewbyid(r.id.price_cart)).gettext().tostring(); model_item = ((textview) finalrowview.findviewbyid(r.id.model_cart)).gettext().tostring(); intent in = new intent(context, addcart_fullimage.class); in.putextra("model", model_item); in.putextra("name", name_item); in.putextra("price", price_item); context.startactivity(in); } }); return rowview; } private bitmap loadimagefromstorage(string path) { try { file f = new file(path, ""); f.canread(); b = bitmapfactory.decodestream(new fileinputstream(f)); return b; } catch (filenotfoundexception e) { e.printstacktrace(); } return null; } public void deleteuser(string username) { final databasehandler db = new databasehandler(context); sqlitedatabase db1 = db.getwritabledatabase(); try { db1.delete("cart", "model = ?", new string[]{username}); //cart_refresh.notifydatasetchanged(); } catch(exception e) { e.printstacktrace(); } { db.close(); } } public void updatetable() { final databasehandler db = new databasehandler(context); sqlitedatabase db1 = db.getwritabledatabase(); try { db.updatecontact(new cart(name, price,image_new,model, product,value)); } catch (exception e) { } }
any appreciable.
thanks.
your biggest issue is, you're using fields instead of local variables.
quant = holder.quantity.gettext().tostring();
updates quant
every call getview()
so
@override public void aftertextchanged(editable s) { value = s.tostring().trim(); quant.replace(holder.quantity.gettext().tostring(), value); updatetable(); }
will use text field last bound data , not the 1 added listener to.
you need take care of scope of variables you're using , introduce consistency model.
i recommend complete rewrite of adapter without using fields.
Comments
Post a Comment