Android:动态更改ListView项目背景颜色

时间:2014-07-08 10:14:22

标签: android listview bluetooth

我在动态更改ListView中的文字颜色方面遇到了问题。 我有一个ListView的蓝牙设备名称,我需要更改可见设备的背景颜色。我正在运行broadcastReceiver,当设备名称等于列表中的位置时,我正在改变背景颜色。这是我的代码:

devicesList = (ListView) findViewById(R.id.pairedDevicesListView);

    adapter = new ArrayAdapter(this, R.layout.text_view_layout, DatabaseHandler.getInstance().getCounters(false));
    devicesList.setAdapter(adapter);

broadcastReceiver

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Broadcast Receiver");
        String action = intent.getAction();
        ArrayList<String> devicesInDB = DatabaseHandler.getInstance().getCounters(false);
        if(BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            System.out.println(device.getName());
            availableDevices.add(device.getName());
            adapter.notifyDataSetChanged();
            for(int i = 0; i < devicesList.getChildCount(); i++) {
                if(device.getName().equals(devicesInDB.get(i))) {
                    devicesList.getChildAt(i).setBackgroundColor(Color.GREEN);
                }
            }

        }
    }
};

检测设备工作正常,但背景颜色看起来像是随机更改的。有人能帮助我吗?

0 个答案:

没有答案