如何正确更新RecyclerView列表?

时间:2018-07-20 15:27:33

标签: java android android-recyclerview android-things

在遍历搜索后,我无法用正确的信号电平和电平更新 Android Things 显示。搜索栏基本上会获取WifiData的ArrayList,并制作一个新的Wifi数据过滤列表以列表形式显示。但是一旦进行搜索,所有过滤的列表项都不会更新其信号电平和电平,而Android Things上的显示会更新。更糟糕的是,经过搜索并从EditText中删除了文本之后,原始列表又回来了,也不会正确更新显示。

每10秒更新一次Wifi数据列表的代码:

 public void updateWifiData() {
    mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if (receiverWifi == null) {
        receiverWifi = new WifiReceiver();
    }

    registerReceiver(receiverWifi, new IntentFilter(
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    mainWifi.startScan();

    doInBack();
}

public void doInBack() {
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            updateWifiData();
        }
    }, WIFI_UPDATE_DELAY);
}

接收WifiData并将其输入到列表中的代码:

class WifiReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        sb = new StringBuilder();
        List<ScanResult> wifiList = mainWifi.getScanResults();
        wifiData.clear();

        for (int i = 0; i < wifiList.size(); i++) {

            ScanResult aResult = wifiList.get(i);

            WifiData data = new WifiData();

            data.BSSID = aResult.BSSID;
            data.SSID = aResult.SSID;
            data.level = aResult.level;
            data.signalLevel = WifiManager.calculateSignalLevel(aResult.level, 7);
            data.channelWidth = aResult.channelWidth;
            data.frequency = aResult.frequency;

            wifiData.add(data);
        }

        Collections.sort(wifiData);
        Collections.reverse(wifiData);
        recyclerViewAdapter.notifyDataSetChanged();
        layoutManager.scrollToPosition(getPositionOfSelectedWifi());

        //TODO Fix index here maybe
        //update Display on refresh
        if (!(selectedWifi == null)) {

            int pos = -1;
            for (int i = 0; i < wifiData.size(); i++) {
                WifiData newData = wifiData.get(i);
                if (newData.BSSID.equalsIgnoreCase(selectedWifi.BSSID)) {
                    pos = i;
                    break;
                }
            }

            if (pos != -1) {
                WifiData newData = wifiData.get(pos);
                if (!(newData.level == selectedWifi.level)) {
                    updateDisplay(newData.level);
                    updateStrip(newData);
                }
            }
        }
    }
}

更新 Android Things 上显示的代码:

public void updateDisplay(float level) {

    try {

        if (mDisplay == null) {
            mDisplay = RainbowHat.openDisplay();
            mDisplay.setEnabled(true);
        }

        mDisplay.display(level);
        Log.d(TAG, "Initialized I2C Display");
    } catch (IOException e) {
        throw new RuntimeException("Error initializing display", e);
    }
}

public void updateStrip(WifiData wifiData) throws RuntimeException {
    try {

        if (mLedstrip == null) {
            mLedstrip = RainbowHat.openLedStrip();
        }
        mLedstrip.setBrightness(LEDSTRIP_BRIGHTNESS);

        Log.d("TEST", "signal: " + wifiData.signalLevel);

        int[] colors = fillColors((int) wifiData.signalLevel);

        Log.d("TEST", "output: " + Arrays.toString(colors));
        mLedstrip.write(colors);
        // Because of a known APA102 issue, write the initial value twice.
        mLedstrip.write(colors);
        Log.d(TAG, "Initialized SPI LED strip");

    } catch (IOException e) {
        throw new RuntimeException("Error initializing LED strip", e);
    }
}

为避免放置大量代码,除非有人需要看到它,否则我将省略我的RecyclerView适配器。我希望你们能帮助我解决我的问题!谢谢!

0 个答案:

没有答案