我在动态更改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);
}
}
}
}
};
检测设备工作正常,但背景颜色看起来像是随机更改的。有人能帮助我吗?