java - 适配器确实有问题

标签 java android

我有下一个适配器:

public class CityAdapter extends ArrayAdapter<String> {     

    public CityAdapter(Context context, int resource) {
        super(context, resource, Cities.TITLES);

    }    

    public int getCount() {
       return Cities.ARRAY.length;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {    
        View v = convertView;    
        if (v == null) {    
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.city_list_row, null);    
        }

        TextView title = (TextView) v.findViewById(R.id.cityTitle);    
        title.setText(Cities.ARRAY[position].getName());    
        if (position==4) {    
            title.setTextColor(Color.parseColor("#ff0000"));
        }    
        return v;    
    }    
}

请仔细看看这一行:

if (position==4) 

我不知道怎么做,但该语句也适用于位置 0。

enter image description here

看起来确实不可能。怎么了?另外,如果我像这样更改代码:

if (position==4) {
    title.setText("hey!!!");
    title.setTextColor(Color.parseColor("#ff0000"));
}

我会得到这个:enter image description here

最佳答案

为您的颜色设置添加一个 else case,如下所示

if (position==4) {    
    title.setTextColor(Color.parseColor("#ff0000"));
} 
else {
    title.setTextColor(Color.parseColor("#ffffff"));
}

关于java - 适配器确实有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31207307/

相关文章:

java - 在java中找到字符串中第n次出现的子字符串?

java - Xuggler-Java-如何从字节数组创建数据包?

java - 类路径使用哪些文件?

android - Google Analytics + EasyTracker (Android) 在快速切换时跟踪相同的 Activity

android - 脚本IntrincisConvolve3x3 : Output-Allocation float4 instead of U8_4?

Java - 将默认浮点常量类型从 double 更改为 float

java - 如何避免 Set<Object> 中的重复

android - 无法在 Android Studio 中更改 SDK 路径

android - 处理服务上的 onActivityResult

java - Android 上的语音转文本