我正在使用适用于Android的Estimote SDK。我正在尝试更改官方网站上提供的代码。我想在ListFragment中显示检测到的信标列表(每个信标有一些信息)。反复更新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:致命异常:主 工艺: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) 在com.estimote.sdk.BeaconManager $ IncomingHandler.handleMessage(BeaconManager.java:479) 在android.os.Handler.dispatchMessage(Handler.java:102)在java.lang.reflect.Method.invoke(Native Method) at android.app.ActivityThread.main(ActivityThread.java:5274) .lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:909) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:704)
谢谢你的帮助。
OMG,OMG请杀了我。 – user3075478 2015-04-01 18:34:28
好吧,这是令人难以置信的 - 。 - 现在没关系。就一个问题。这是做到这一点的正确方法吗? – user3075478 2015-04-01 18:36:50
@ user3075478正确的做法是什么?更新列表片段中的项目列表? – osayilgan 2015-04-01 18:59:35