android - notifyDataSetChanged() not working or triggered? -
what i'm trying do:
inserting value edittext database when add button pressed.
populating listview data's database.
problem:
when click add button insert value database,but not populating listview. when reopen application,listview refreshed , displays data's database.
here code:
public class mainactivity extends appcompatactivity { edittext edittext; button add; listview listview; final dbfunctions db=new dbfunctions(mainactivity.this); arrayadapter<string> adapter; string[] values; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //xml inflation edittext= (edittext) findviewbyid(r.id.edittext); add= (button) findviewbyid(r.id.button); listview= (listview) findviewbyid(r.id.listview); values=db.getalldata(); adapter=new arrayadapter<string>(mainactivity.this,android.r.layout.simple_list_item_1,values); listview.setadapter(adapter); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string name = edittext.gettext().tostring(); if (!name.equals("")) { edittext.settext(""); long check = db.insertdata(name); if (check < 0) { toast.maketext(mainactivity.this, "error in insert query", toast.length_short).show(); } else { adapter.notifydatasetchanged(); toast.maketext(mainactivity.this, name + " inserted", toast.length_short).show(); } } else { edittext.seterror("this field empty"); } } }); }
did used adapter.notifydatasetchanged in correct place.
can give me solution this. me alot.
thanks in advance.
your adapter populating listview data gets values array. inserting new data in database not change values array , hence adapter not find change.
try changing values array too. using arraylist instead of arrays better choice.
public class mainactivity extends appcompatactivity { edittext edittext; button add; listview listview; final dbfunctions db=new dbfunctions(mainactivity.this); arrayadapter<string> adapter; arraylist<string> values; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //xml inflation edittext= (edittext) findviewbyid(r.id.edittext); add= (button) findviewbyid(r.id.button); listview= (listview) findviewbyid(r.id.listview); values = new arraylist<string>(arrays.aslist(db.getalldata())); adapter=new arrayadapter<string>(mainactivity.this,android.r.layout.simple_list_item_1,values); listview.setadapter(adapter); add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string name = edittext.gettext().tostring(); if (!name.equals("")) { edittext.settext(""); long check = db.insertdata(name); if (check < 0) { toast.maketext(mainactivity.this, "error in insert query", toast.length_short).show(); } else { values.add(name); adapter.notifydatasetchanged(); toast.maketext(mainactivity.this, name + " inserted", toast.length_short).show(); } } else { edittext.seterror("this field empty"); } } }); }
Comments
Post a Comment