How to save a ListView into a xml file in Android? -
i developing android app capable of detecting ble signals , list them in listview. after scan period of time stop scanning. here code (it same in developers page):
scanbleactivity.class (extends scanbaseactivty):
public class scanbleactivity extends scanbaseactivity { private bluetoothadapter mbluetoothadapter; private boolean mscanning; private handler mhandler = new handler(); // stops scanning after 10 seconds. private static final long scan_period = 20000; /* (non-javadoc) * @see com.zishao.bletest.scanbaseactivity#initscanbluetooth() */ protected void initscanbluetooth() { bluetoothmanager manager = (bluetoothmanager) getsystemservice(context.bluetooth_service); mbluetoothadapter = manager.getadapter(); startscanlen(true); } @override protected void ondestroy() { super.ondestroy(); if (mscanning) { startscanlen(false); } } /** * * @param enable */ private void startscanlen(final boolean enable) { if (enable) { // stops scanning after pre-defined scan period. mhandler.postdelayed(new runnable() { @override public void run() { mscanning = false; mbluetoothadapter.stoplescan(mlescancallback); } }, scan_period); mscanning = true; mbluetoothadapter.startlescan(mlescancallback); } else { mscanning = false; mbluetoothadapter.stoplescan(mlescancallback); } } // device scan callback. private bluetoothadapter.lescancallback mlescancallback = new bluetoothadapter.lescancallback() { @override public void onlescan(final bluetoothdevice device, int rssi, byte[] scanrecord) { adddevice(device, rssi); } };
adddevice implemented in other class, in case scanbaseactivity:
scanbaseactivity (which extends listactivity):
abstract public class scanbaseactivity extends listactivity { protected ledevicelistadapter mledevicelistadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_devices_scan); mledevicelistadapter = new ledevicelistadapter(this, new arraylist<bluetoothdevice>()); this.setlistadapter(mledevicelistadapter); initscanbluetooth(); } /** * start scan bluetooth * */ abstract protected void initscanbluetooth(); @override protected void onlistitemclick(listview l, view v, int position, long id) { bluetoothdevice device = (bluetoothdevice) mledevicelistadapter.getitem(position); parceluuid[] uuids = device.getuuids(); string uuidstring = "getting uuid's " + device.getname() + ";uuid:"; if (null != uuids && uuids.length > 0) { uuidstring += uuids[0].getuuid().tostring(); } else { uuidstring += "empty"; } toast.maketext(this, uuidstring, toast.length_long).show(); } /** * @param device */ protected synchronized void adddevice(final bluetoothdevice device, final int rssi) { runonuithread(new runnable() { @override public void run() { mledevicelistadapter.adddevice(device, rssi); mledevicelistadapter.notifydatasetchanged(); } }); } }
all information detect (name, address, rssi, etc) listed in listview. listview have implemented adapter called baseadapter. part of code of adapter following:
public class ledevicelistadapter extends baseadapter { private list<bluetoothdevice> data; private activity context; private final hashmap<bluetoothdevice, integer> rssimap = new hashmap<bluetoothdevice, integer>(); public ledevicelistadapter(activity context, list<bluetoothdevice> data) { this.data = data; this.context = context; } public synchronized void adddevice(bluetoothdevice device, int rssi) { if(!data.contains(device) ){ data.add(device); } rssimap.put(device, rssi); } @override public int getcount() { return data.size(); } @override public object getitem(int position) { return data.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (null == convertview) { layoutinflater minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = minflater.inflate(r.layout.leaf_devices_list_item, null); convertview.settag(new deviceview(convertview)); } deviceview view = (deviceview) convertview.gettag(); view.init((bluetoothdevice) getitem(position), null); return convertview; } public class deviceview { private textview title; private textview status; private textview type; private textview address; private textview rssivalue; public deviceview(view view) { title = (textview) view.findviewbyid(r.id.device_name); status = (textview) view.findviewbyid(r.id.device_status_txt); type = (textview) view.findviewbyid(r.id.device_type_txt); address = (textview) view.findviewbyid(r.id.device_address_txt); rssivalue = (textview) view.findviewbyid(id.signal_intensity_txt); }
when scan finishes save listview file (if possible xml file) don´t know , code should use in project save it. imortant use timestamp in file know when saved. can me or give me clues??
new: code edited show code scanbaseactivity , scanbleactivity!!!
i see 1 problem constructor.
public ledevicelistadapter(activity context, list<bluetoothdevice> data) { //edit:: //add following line super(context, r.layout.leaf_devices_list_item, data); this.data = data; this.context = context; }
calling super constructor passes reference of data baseadapter. then, baseadapter able handle changes in list.
Comments
Post a Comment