我正在创建具有以下功能的应用程序。
我有一个自定义类来存储传感器数据(SenseData)和自定义视图来显示传感器名称和读数。
我写了一个自定义的Adapter类,它接受一个SenseData列表来填充我活动中的列表视图组件。
我想做的是,获取SenseData列表。
我尝试了大约一个星期但没有得到任何解决方案。 如果有人能给我一个暗示,我将非常感激。
编辑:
适配器类
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
}
然后我收到带有传感器名称为动作的过滤器的广播。但我无法找到一种方法来返回广播接收器中的列表。 SenseData类具有设置和获取数据的setter和getter。
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;
}
}
接收者已在活动中注册
IntentFilter filter = new IntentFilter("accelerometer");
SensorBroadcastReceiver receiver = new SensorBroadcastReceiver();
registerReceiver(receiver, filter);
我可以向List<SenseData> sensordatalist
添加SensorBroadcastReceiver
但是如何实时获取值?
非常感谢。