我使用的是Estimote SDK for Android。我试图更改官方网站上提供的代码。我想在ListFragment中显示检测到的信标列表(包含每个信标的一些信息)。
在我的主要活动中,我开始所有必要的(服务,听众......),特别是我有这个onCreate(...)方法:
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 the list of the beacons in the ListFragment in some way
}
});
}
如果我理解正确,每隔一段时间就会连续调用这种方法。
这是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;
}
// this does not work if i call it from the 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;
}
}
}
正如你所看到的,我试图将一个公共方法放在ListFragment中,并将其称为onCreate中的监听器,但这并不起作用。特别是我得到一个例外:
04-01 20:14:04.029 18052-18052 / it.loris.beaconsdetector E / AndroidRuntime:FATAL EXCEPTION:main 过程:it.loris.beaconsdetector,PID:18052 java.lang.UnsupportedOperationException at java.util.Collections $ UnmodifiableCollection.clear(Collections.java:936) at it.loris.beaconsdetector.DiscoveredBeacons.updateList(DiscoveredBeacons.java:40) at it.loris.beaconsdetector.BeaconsDetector $ 1.onBeaconsDiscovered(BeaconsDetector.java:41) at 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) at 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)
感谢您的帮助。
答案 0 :(得分:1)
这是一个容易犯的错误。您正在尝试清除信标列表,而不是列表,即从方法中收到的列表实例。使用“this”访问列表片段中的信标。并添加从方法中收到的信标。这应该解决它。
// this does not work if i call it from the main Activity
public void updateList(List<Beacon> beacons) {
// Call this to refer your variable in List fragment
this.beacons.clear();
this.beacons.addAll(beacons);
((MyListAdapter) getListAdapter()).notifyDataSetChanged();
}
答案 1 :(得分:0)
尝试将此添加到 getView()
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.beacon_row, null);
} else { v = convertView; }