我需要刷新列表视图,而无需清除和闪烁,仅显示新项目。但是我无法立即执行此操作,因为我在几秒钟内就使项目不同步。由于在程序的一开始就清除了适配器,所以该代码无法正常工作。

private void scanLeDevice(final boolean enable) {
    if (enable) {
        mLeDeviceListAdapterNew.clear();
        //mLeDeviceListAdapterNew.notifyDataSetChanged();

        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);

                runOnUiThread(new Runnable(){
                    @Override
                    public void run() {
                        mLeDeviceListAdapterNew.notifyDataSetChanged();
                        scanLeDevice(true);
                        //invalidateOptionsMenu();
                    }
                });
            }
        }, SCAN_PERIOD);


//Here my adapter updates with new items,
//but it's shouldn't be displayed until I call adapter
//refresh data in SCAN_PERIOD

最佳答案

您可以在设置适配器后立即调用notifyDataSetChanged();以反映对列表的更改。例如

ListView modeList = new ListView(getActivity());
            ArrayAdapter<CharSequence> adapter = ArrayAdapter
                    .createFromResource(getActivity(), R.array.PGA_Arrays,
                            R.layout.simple_spinner_item);
            modeList.setAdapter(adapter);
            adapter.notifyDataSetChanged();


希望对您有所帮助。

关于java - 更新ListView而不清除和闪烁,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22328710/

10-14 05:17