重复更新 ListFragment 中的列表

Repeatedly update a list in the ListFragment

我正在为 Android 使用 Estimote SDK。我正在尝试更改官方网站上提供的代码。我想在 ListFragment 中显示检测到的信标列表(每个信标都有一些信息)。

在我的主要 Activity 中,我启动了所有必要的(服务、侦听器..),特别是我有这个 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 中放置一个 public 方法,并在 onCreate 中将其称为侦听器,但这不起作用。特别是我得到一个例外:

04-01 20:14:04.029 18052-18052/it.loris.beaconsdetector E/AndroidRuntime:致命异常:main 进程:it.loris.beaconsdetector,PID:18052 java.lang.UnsupportedOperationException 在 java.util.Collections$UnmodifiableCollection.clear(Collections.java:936) 在 it.loris.beaconsdetector.DiscoveredBeacons.updateList(DiscoveredBeacons.java:40) 在 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) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5274) 在 java.lang.reflect.Method.invoke(本机方法) 在 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)

感谢您的帮助。

这是一个容易犯的错误。您正在尝试清除信标列表,而不是您的列表,即您从该方法收到的列表实例。使用 "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();
}

尝试将其添加到 getView()

if (convertView == null) {
            LayoutInflater vi = (LayoutInflater)  getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.beacon_row, null);
        } else { v = convertView; }