更新listview而不清除和闪烁

raogr8fs  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(372)

我需要刷新我的listview没有清除和闪烁,只是显示新的项目。但我不能一次就这么做,因为我需要在几秒钟内异步获取项目。由于在过程开始时清除了适配器,所以此代码不能正常工作。

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
mwecs4sa

mwecs4sa1#

你可以打电话 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();

希望这能有所帮助。

nzk0hqpo

nzk0hqpo2#

您需要在适配器中使用setdata方法来更新数据。之前打过电话 notiftyDataSetChanged .
公共类myadapter扩展baseadapter{

public MyAdapter(...) {
    super(...);
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    .....
    return view;

}

private void setData() {
    ...
    notifyDataSetChanged();
}

}

相关问题