java - simple adapter cant update database -
hey guys have made listview of data database using simple adapter dont know how update everytime database changes.any help?the notifydatasetchanged(); method doesnt work , cant find out do.
mainactivity
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_general_discussion); db = new sqlitehandler(this); arraylist<hashmap<string, string>> emaillist = db.getmessage(); if (emaillist.size() != 0) { listadapter adapter = new simpleadapter(mainactivity.this, emaillist, r.layout.raw_layout, new string[]{"user_posted", "post", "posted_at", "post_id"}, new int[]{ r.id.text_user_name, r.id.text_user_post, r.id.text_user_date, r.id.text_user_number}); setlistadapter(adapter); }
helperclass
public arraylist<hashmap<string, string>> getmessage(){ arraylist<hashmap<string, string>> message = new arraylist<hashmap<string, string>>(); string selectquery = "select * " + table_posting; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); if ( cursor.movetofirst()) { { hashmap<string, string> map = new hashmap<string, string>(); map.put("post_id", cursor.getstring(0)); map.put("user_posted", cursor.getstring(1)); map.put("post", cursor.getstring(2)); map.put("posted_at", cursor.getstring(3)); message.add(map); } while (cursor.movetonext()); } return message; }
declare adapter & list global level
class example{ listadapter adapter; arraylist<hashmap<string, string>> emaillist; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_general_discussion); db = new sqlitehandler(this); emaillist = db.getmessage(); if (emaillist.size() != 0) { adapter = new simpleadapter(mainactivity.this, emaillist, r.layout.raw_layout, new string[]{"user_posted", "post", "posted_at", "post_id"}, new int[]{ r.id.text_user_name, r.id.text_user_post, r.id.text_user_date, r.id.text_user_number}); setlistadapter(adapter); }
when want update
adapter.notifydatasetchanged();
Comments
Post a Comment