java - Listview isn't updating in Fragment -
i know sure updatefromdatabase()
function works, i've used print statements see entries put mcoordinatesarray
there , not empty strings. when restart app, fragment never populates list view items in database. think has fragment lifecycle, have no idea.
additionally, when don't restart app , run first time list view runs fine. when rotate or restart app, list view no longer populates.
public class localfragment extends fragment{ private listview mlocallist; private arrayadapter<string> adapter; private arraylist<string> mcoordinatesarray; private broadcastreceiver mbroadcastreceiver; private locationbasehelper mdatabase; private dateformat dateformat; private string datestring; @override public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view v = inflater.inflate(r.layout.fragment_local,container,false); // sqlite setup mdatabase = new locationbasehelper(getactivity()); mlocallist = (listview) v.findviewbyid(r.id.lv_local); mcoordinatesarray = new arraylist<>(); adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, mcoordinatesarray); if(!mdatabase.size().equals("0")){ updatefromdatabase(); } mlocallist.setadapter(adapter); return v; } @override public void onresume() { super.onresume(); if(mbroadcastreceiver == null){ mbroadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { dateformat = new simpledateformat("mm/dd hh:mm:ss a"); datestring = dateformat.format(new date()); string[] data = intent.getstringextra("coordinates").split(" "); mdatabase.insertentry(datestring,data[0],data[1]); system.out.println(mdatabase.size()); mcoordinatesarray.add(datestring + " " + data[0] + " " + data[1]); adapter.notifydatasetchanged(); } }; } getactivity().registerreceiver(mbroadcastreceiver, new intentfilter("location_update")); } @override public void ondestroy() { super.ondestroy(); if(mbroadcastreceiver!=null){ getactivity().unregisterreceiver(mbroadcastreceiver); } } // method can used update array holding coordinates local database private void updatefromdatabase(){ //mcoordinatesarray.clear(); mcoordinatesarray = mdatabase.getentiredatabase(); adapter.notifydatasetchanged(); } }
here's helper class, in case, don't think problem here.
public class locationbasehelper extends sqliteopenhelper { private static final int version = 1; private static final string database_name = "locationbase.db"; public locationbasehelper(context context) { super(context, database_name, null, version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table " + locationtable.name + " (" + locationtable.cols.date_time + " text, " + locationtable.cols.latitude + " text, " + locationtable.cols.longitude + " text )" ); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void insertentry(string date_time, string latitude, string longitude){ sqlitedatabase db = this.getwritabledatabase(); contentvalues content = new contentvalues(); content.put(locationtable.cols.date_time,date_time); content.put(locationtable.cols.latitude,latitude); content.put(locationtable.cols.longitude,longitude); db.insert(locationtable.name,null,content); } public arraylist<string> getentiredatabase(){ sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery("select * " + locationtable.name,null); cursor.movetofirst(); arraylist<string> values = new arraylist<>(); do{ string value = (string) cursor.getstring(cursor.getcolumnindex(locationtable.cols.date_time)) + " " + (string) cursor.getstring(cursor.getcolumnindex(locationtable.cols.latitude)) + " " + (string) cursor.getstring(cursor.getcolumnindex(locationtable.cols.longitude)); values.add(0,value); }while(cursor.movetonext()); return values; } public string size(){ sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery("select count(*) " + locationtable.name,null); cursor.movetofirst(); return cursor.getstring(0); } }
by calling mcoordinatesarray = mdatabase.getentiredatabase();
changing reference of mcoordinatesarray
, , adapter still holding old reference, not see changes.
instead of creating new instance of mcoordinatearray
, should rather update values contains, like:
mcoordinatearray.clear(); mcoordinatearray.addall(mdatabase.getdata()); adapter.notifydatasetchange();
that way changing data referenced adapter, instead of creating new set of data adapter not aware of.
Comments
Post a Comment