问题描述
我要创建哪些具有以下功能的应用程序。
I'm creating an application which has following functionality.
- 用户选择它们是在设备中可用的传感器。
- 的UI显示所选传感器名称和读数在ListView。(值更新实时)
我有一个自定义类来存储传感器数据(检测数据)和自定义视图来显示传感器名称和读数。
I have a custom class to store sensor data (SenseData) and custom view to display sensor name and readings.
我写了一个自定义的适配器类需要检测数据的列表,在我的活动来填充列表视图组件。
I wrote a custom Adaptor class which takes a list of SenseData to populate a list view component in my activity.
我想要做的就是,拿检测数据的列表。
What I want to do is, take a list of SenseData.
我想这一个星期左右,并没有得到任何的解决方案。
我会很感激,如果有人可以给我一个提示。
I'm trying this for about a week and didn't get any solutions.I'll be very grateful if someone can give me a hint.
编辑:
适配器类
public class SensorAdaptor extends BaseAdapter{
Context context;
List<SenseData> senseData;
public SensorAdaptor(Context context, List<SenseData> senseData){
this.context = context;
this.senseData = senseData;
}
@Override
public int getCount() {
return senseData.size();
}
@Override
public Object getItem(int position) {
return senseData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
if(convertView == null){
view = inflater.inflate(R.layout.view_sensor, parent, false);
holder = new ViewHolder();
holder.name = (TextView) view.findViewById(R.id.senseName);
holder.data = (TextView) view.findViewById(R.id.senseReading);
view.setTag(holder);
}else{
view = convertView;
holder = (ViewHolder) view.getTag();
}
SenseData data = senseData.get(position);
holder.name.setText(data.getSensorName());
holder.data.setText(data.getSensorValues());
return null;
}
public class ViewHolder{
TextView name;
TextView data;
}
}
onSensorChanged方法
@Override
public void onSensorChanged(SensorEvent event) {
this.event = event;
//SupportedSensors contains the predefined sensor types. sensor is a SenseData type object
Intent filter = new Intent(new
SupportedSensors().getType(event.sensor.getType()));
filter.putExtra("value", event.values[0]);
ctx.sendBroadcast(filter); //ctx is the context
}
然后我收到的广播与具有传感器的名称作为行动的过滤器。但我无法找到一个方法来返回广播接收器的列表。检测数据类具有getter和setter方法来设置和获取数据。
Then I receive broadcasts with filters which have sensor names as action. But I couldn't find out a way to return a list in Broadcast receiver. SenseData class has setters and getters to set and get data.
public class SensorBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch(intent.getAction()){
case "accelerometer":
System.out.println("Accelerometer");
break;
case "gyroscope":
System.out.println("Gyroscope");
break;
case "gravity":
System.out.println("Gravity");
break;
case "proximity":
System.out.println("Proximity");
break;
case "pressure":
System.out.println("Pressure");
break;
case "light":
System.out.println("Light");
break;
case "magnetometer":
System.out.println("Magnetometer");
break;
case "rotation":
System.out.println("Rotation Vector");
break;
}
}
的接收机被登记在活动
The receiver is registered in the Activity
IntentFilter filter = new IntentFilter("accelerometer");
SensorBroadcastReceiver receiver = new SensorBroadcastReceiver();
registerReceiver(receiver, filter);
我可以添加列表与LT;检测数据&GT; sensordatalist
到 SensorBroadcastReceiver
但如何获取值实时?
非常感谢你。
推荐答案
我能够通过以下方法来解决我的问题....
I was able to solve my problem by using the following method....
-
创建类
RealTimeSensorReader
它读取传感器。
public void onSensorChanged(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();}
定义一个单独的类 TempStore
其中包含两个静态字段
一个的ConcurrentHashMap
和的LinkedList
这是店里传感器
数据并分别填充的ListView。
Define a separate class TempStore
which contains two static fieldsa ConcurrentHashMap
and a LinkedList
which are for store sensordata and to populate the ListView separately.
public class TempStore {
public static ConcurrentMap<String, RealTimeSensor> sensor = new ConcurrentHashMap<>();
public static ArrayList<RealTimeSensor> realTimeSensors = new ArrayList<>();
在onSensorChanged方法,我把传感器值与传感器
命名为关键,以HashMap中。并发送一个广播,只要一
传感器被改变。
In onSensorChanged method I put the sensor values with the sensorname as the key to the HashMap. And send a broadcast whenever asensor is changed.
创建了一个广播监听器,听的广播,并
清除当前LinkedList的值,并添加设置的值
HashMap中。
Created a broadcast listener which listen to that broadcast, andclear the current LinkedList values and add the values set of theHashMap.
TempStore.realTimeSensors.clear();
TempStore.realTimeSensors.addAll(TempStore.sensor.values());
我需要知道的是,这种方法是正确的还是错误的。
What I need to know is whether this method is correct or wrong.
非常感谢你。
这篇关于显示选定的传感器值列表视图实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!