Displaying Selected Sensor Values In A List View Realtime
I'm creating an application which has following functionality. User selects the sensors which are available in the device. The UI shows the selected Sensor Name and readings in a
Solution 1:
I was able to solve my problem by using the following method....
Create a class
RealTimeSensorReader
which reads the sensors.publicvoidonSensorChanged(SensorEvent event) { RealTimeSensor realTimeSensor = new RealTimeSensor(); realTimeSensor.setName(AvailableSensors.getType(event.sensor.getType()).toUpperCase()); realTimeSensor.setValueX(event.values[0]+""); realTimeSensor.setValueY(event.values[1]+""); realTimeSensor.setValueZ(event.values[2]+""); System.out.println(values); TempStore.sensor.put(AvailableSensors.getType(event.sensor.getType()), realTimeSensor); Intent intent = new Intent(); intent.setAction("sensor"); context.sendBroadcast(intent); adaptor.notifyDataSetChanged();}
Define a separate class
TempStore
which contains two static fields aConcurrentHashMap
and aLinkedList
which are for store sensor data and to populate the ListView separately.publicclassTempStore { publicstaticConcurrentMap<String, RealTimeSensor> sensor = new ConcurrentHashMap<>(); publicstaticArrayList<RealTimeSensor> realTimeSensors = new ArrayList<>();
In onSensorChanged method I put the sensor values with the sensor name as the key to the HashMap. And send a broadcast whenever a sensor is changed.
Created a broadcast listener which listen to that broadcast, and clear the current LinkedList values and add the values set of the HashMap.
TempStore.realTimeSensors.clear(); TempStore.realTimeSensors.addAll(TempStore.sensor.values());
What I need to know is whether this method is correct or wrong.
Thank you very much
Post a Comment for "Displaying Selected Sensor Values In A List View Realtime"