android - Repeatedly update a list in the ListFragment -
i'm using estimote sdk android. i'm trying change provided code on official site. want show list of detected beacons in listfragment (with information each beacon).
in main activity start necessary (service, listeners..), , in particular have oncreate(...) method:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); beaconmanager.setranginglistener(new beaconmanager.ranginglistener() { @override public void onbeaconsdiscovered(region region, list<beacon> beacons) { // update list of beacons in listfragment in way } }); }
if understood correctly, method called continuosly every fixed time.
this code of listfragment:
public class discoveredbeacons extends listfragment { private list<beacon> beacons = new arraylist<beacon>(); private beacon beacon; private view mcontentview = null; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); this.setlistadapter(new mylistadapter(getactivity(), r.layout.beacon_row, beacons)); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mcontentview = inflater.inflate(r.layout.beacons_list, null); return mcontentview; } // not work if call main activity public void updatelist(list<beacon> beacons) { beacons.clear(); beacons.addall(beacons); ((mylistadapter) getlistadapter()).notifydatasetchanged(); } private class mylistadapter extends arrayadapter<beacon> { private list<beacon> items; public mylistadapter(context context, int textviewresourceid, list<beacon> items) { super(context, textviewresourceid, items); this.items = items; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if (v == null) { layoutinflater vi = (layoutinflater) getactivity().getsystemservice( context.layout_inflater_service); v = vi.inflate(r.layout.beacon_row, null); } beacon beacon = items.get(position); if (beacon != null) { textview number = (textview) v.findviewbyid(r.id.number); textview rssi = (textview) v.findviewbyid(r.id.rssi); textview power = (textview) v.findviewbyid(r.id.power); textview distance = (textview) v.findviewbyid(r.id.distance); if(number != null) { number.settext("beacon number " + (position + 1)); } if (rssi != null) { rssi.settext("rssi: " + beacon.getrssi()); // int } if (power != null) { power.settext("power: " + beacon.getmeasuredpower()); // int } if(distance != null) { distance.settext("distance: " + utils.computeaccuracy(beacon)); } } return v; } } }
as can see, tried put public method in listfragment, , call listener inside oncreate, doesn't work. in particular exception:
04-01 20:14:04.029 18052-18052/it.loris.beaconsdetector e/androidruntime﹕ fatal exception: main process: it.loris.beaconsdetector, pid: 18052 java.lang.unsupportedoperationexception @ java.util.collections$unmodifiablecollection.clear(collections.java:936) @ it.loris.beaconsdetector.discoveredbeacons.updatelist(discoveredbeacons.java:40) @ it.loris.beaconsdetector.beaconsdetector$1.onbeaconsdiscovered(beaconsdetector.java:41) @ com.estimote.sdk.beaconmanager$incominghandler.handlemessage(beaconmanager.java:479) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5274) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:909) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:704)
thank help.
it's easy mistake. trying clear list of beacons, not list, list instance receive method. use "this" access beacon in list fragment. , add beacons receive method. should fix it.
// not work if call main activity public void updatelist(list<beacon> beacons) { // call refer variable in list fragment this.beacons.clear(); this.beacons.addall(beacons); ((mylistadapter) getlistadapter()).notifydatasetchanged(); }
Comments
Post a Comment