Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:动态更改ListView项目背景颜色_Android_Listview_Bluetooth - Fatal编程技术网

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

Android:动态更改ListView项目背景颜色,android,listview,bluetooth,Android,Listview,Bluetooth,我在动态更改列表视图中的文本颜色时遇到问题。 我有一个带有蓝牙设备名称的列表视图,我需要更改可见设备的背景色。我正在运行broadcastReceiver,当设备名等于列表中的位置时,我正在更改背景色。这是我的代码: devicesList = (ListView) findViewById(R.id.pairedDevicesListView); adapter = new ArrayAdapter(this, R.layout.text_view_layout, DatabaseH

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

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

    adapter = new ArrayAdapter(this, R.layout.text_view_layout, DatabaseHandler.getInstance().getCounters(false));
    devicesList.setAdapter(adapter);
广播接收器

    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);
                }
            }

        }
    }
};
private final BroadcastReceiver=new BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
System.out.println(“广播接收器”);
String action=intent.getAction();
ArrayList devicesInDB=DatabaseHandler.getInstance().getCounters(false);
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
System.out.println(device.getName());
add(device.getName());
adapter.notifyDataSetChanged();
对于(int i=0;i

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

由于Android重用视图,这种方法似乎不太可靠。我建议将信息传递给适配器,在适配器中的
getView
方法上做一些if语句来更改特定视图中的颜色,然后调用
adapter.notifyDataSetChanged()。谢谢,但我解决了这个问题,没有在适配器中重写getView:)