android - 更改 ArrayAdapter ListView Android 中的特定行

标签 android listview android-arrayadapter

我正在尝试根据不同的状态更改特定行的颜色。这是我目前的代码。

 public View getView(int position, View convertView, ViewGroup parent) {

  View row=convertView;

     if (row==null) {                                                    
         LayoutInflater inflater=getLayoutInflater();

         row=inflater.inflate(R.layout.doit, parent, false);
     }

     TextView label = (TextView) row.findViewById(R.id.mess);

     label.setText(ArrayAdapter.getItem(position));

     switch(mState){
     case STATE1:

      label.setTextColor(Color.WHITE);
      break;
     case STATE2:
      label.setTextColor(Color.BLACK);
      break;
     case STATE3:
      label.setTextColor(Color.YELLOW);
      break;
     }


     return(row);
 }

该代码有点工作..但它改变了所有的行。有什么想法吗?

最佳答案

因此 android 每次都会重复使用 View ,这就是为什么您会看到它影响所有行。您需要做的是为每种情况明确设置颜色。也许向您的 switch 语句添加一个“默认”案例,以便将其设置为您在布局中的默认值?

关于android - 更改 ArrayAdapter ListView Android 中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3300565/

相关文章:

java - 我该如何格式化这个日期?

android - ScrollView 的滚动在 bottomsheet 内不起作用?

android - 重启android设备Button的onClick事件

android - 当默认键盘打开时,EditText 在 ListView 中失去焦点

Android改造解析List中嵌套的json响应

android - 如何在填充列表后以编程方式更改列表元素的高度

android - 如何在android中保存图像和表面 View ?

java - 如何在自定义ListView中使用Intent

android - 复选框会在 Android 中自动选中

java - 使用自定义 ArrayAdapter 时无法解析方法 super