如何替换Android中正在更新的Textview的颜色?

时间:2014-03-14 15:49:22

标签: android colors textview

我在Android中开发了一款能够检测蓝牙信号的应用程序,并在Listview中显示它们。它显示了名称,地址和rssi值。对于这最后一个参数,我已经完成了这样,以便不断更新。问题是我希望rssi的值在更新其值时在两种或更多颜色之间交替;例如,黑色值为-83dBm,红色更新值为-81dBm。我尝试过一种方法,但是当它运行时,由于试图改变颜色,它没有显示任何值。这是我使用的代码:

新:代码编辑!!!

public class BleDevicesAdapter extends BaseAdapter {
private final LayoutInflater inflater;

private final ArrayList<BluetoothDevice> leDevices;
private final HashMap<BluetoothDevice, Integer> rssiMap = new HashMap<BluetoothDevice, Integer>();

public BleDevicesAdapter(Context context) {
    leDevices = new ArrayList<BluetoothDevice>();
    inflater = LayoutInflater.from(context);
}

public void addDevice(BluetoothDevice device, int rssi) {
    if (!leDevices.contains(device)) {
        leDevices.add(device);
    }
    rssiMap.put(device, rssi);
}

public BluetoothDevice getDevice(int position) {
    return leDevices.get(position);
}

public void clear() {
    leDevices.clear();
}

public Integer getRSSI(int j){
    BluetoothDevice device = leDevices.get(j);
    return rssiMap.get(device);
}

@Override
public int getCount() {
    return leDevices.size();
}

@Override
public Object getItem(int i) {
    return leDevices.get(i);
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder viewHolder;
    // General ListView optimization code.
    if (view == null) {
        view = inflater.inflate(R.layout.listitem_device, null);
        viewHolder = new ViewHolder();
        viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
        viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
        viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    BluetoothDevice device = leDevices.get(i);
    final String deviceName = device.getName();
    if (deviceName != null && deviceName.length() > 0)
        viewHolder.deviceName.setText(deviceName);
    else
        viewHolder.deviceName.setText(R.string.unknown_device);
        viewHolder.deviceAddress.setText(device.getAddress());

        if(viewHolder.deviceRssi.getCurrentTextColor()==0x000000){
        viewHolder.deviceRssi.setTextColor(0xFF0000);
        viewHolder.deviceRssi.setText(""+rssiMap.get(device)+" dBm");

        }
        else {
                    viewHolder.deviceRssi.setTextColor(0x000000);
            viewHolder.deviceRssi.setText(""+rssiMap.get(device)+" dBm");

        }

    return view;
}

private static class ViewHolder {
    TextView deviceName;
    TextView deviceAddress;
    TextView deviceRssi;
}

有谁知道如何解决这个问题?请帮助!!!!

1 个答案:

答案 0 :(得分:0)

这很简单。

Adapter内:

if(value > yourdesiredmax) TextView.setTextColor(Color.RED);
else TextView.setTextColor(Color.BLACK);

ListView Adapters内使用 if-statemens 时,提供 else-statement 。因此,原因是ListView重新使用Views,事情可能会搞砸。