2014-09-29 45 views
2

我想更改我的列表视图的背景颜色。 从数字背景应该由状态Android的更改列表中的项目与自定义适配器的列表视图

1 = red 
2 = grey 
3 = green 
4 = yellow 

这就是从我的自定义适配器更改代码(不工作):

TextView number = (TextView) rowView.findViewById(R.id.tv_number); 

switch(Integer.parseInt(status[position])){ 
case 1: number.setBackgroundColor(0xff0000); 
     break; 

case 2: number.setBackgroundColor(0xdfdfdf); 
     break; 

case 3: number.setBackgroundColor(0x00ff00); 
     break; 

case 4: number.setBackgroundColor(0xffff00); 
     break; 

default : 
     break; 
} 

为什么它不工作?

+0

什么是status []?您可以直接通过位置切换案例。 – 2014-09-29 10:49:21

+0

status [position] = 1,2,3或4改变背景颜色 – rapha31 2014-09-29 10:55:24

+0

你想改变listview背景或列表项背景吗? – 2014-09-29 10:59:16

回答

2

当你的一个小部件设置背景,你还需要alpha通道添加到您希望使用由于Android需要的颜色是在格式#ARGB,例如#AARRGGBB颜色,其中A是alpha通道,并且RGB是红绿蓝。

所以,你的情况,如果你想透明(前缀)alpha通道,那么你就需要为情况1做到这一点:

number.setBackgroundColor(0x00ff0000); 

或者,不透明通道(FF前缀),请执行此操作:

number.setBackgroundColor(0xffff0000); 
+0

非常感谢您的解决方案 – rapha31 2014-09-29 11:13:21

+0

不客气。很高兴我能帮上忙。 ;) – ChuongPham 2014-09-29 11:13:48

1
int cc[] = {R.color.red,r.color.blue} 

设置在getview方法TextView的背景作为

number.setBackgroundColor(cc[position]); 
1

尝试这;用这个替换你的代码。

TextView number = (TextView) rowView.findViewById(R.id.tv_number); 

    switch(Integer.parseInt(status[position])){ 
    case 1: number.setBackgroundColor(Color.RED); 
      break; 

    case 2: number.setBackgroundColor(Color.GRAY); 
      break; 

    case 3: number.setBackgroundColor(Color.GREEN); 
      break; 

    case 4: number.setBackgroundColor(Color.YELLOW); 
      break; 

    default : 
      break; 
    } 
+0

rapha31检查编辑的答案。 – Sats 2014-09-29 11:00:00

+0

它也可以工作,但ChuongPham的解决方案更好。 – rapha31 2014-09-29 11:15:08

+0

@ rapha31但在这里你问setbackgroundcolor不像alpha,好吧 – Sats 2014-09-29 11:19:57

0

你的状态函数返回什么和rowView是什么?有点难说,不说更多。

其他一些建议:

应避免硬编码值这样的,它是在你的RE /值/文件夹使用colors.xml文件很好的做法:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="primary_white">#ffffff</color> 
</resources> 

然后引用你的资源就像字符串一样,但是R.color.primary_white

这样的幻数也是不好的做法。你的文件的顶部尝试设置一些静态值:

static int STATUS_RED = 1; 

....

case STATUS_RED: 

这里是在ListView变化的背景和样式行的教程:

http://webdeveloperpadawan.blogspot.co.uk/2014/09/android-listview-with-differing-rows.html

0

试试这个 -

TextView number = (TextView) rowView.findViewById(R.id.tv_number); 

switch(position+1){ 
case 1: number.setBackgroundColor(Color.RED); 
     break; 

case 2: number.setBackgroundColor(Color.GRAY); 
     break; 

case 3: number.setBackgroundColor(Color.GREEN); 
     break; 

case 4: number.setBackgroundColor(Color.YELLOW); 
     break; 

default : 
     break; 
} 
0

使用此代码在getView方法..

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

     switch (position % 4) { 
       case 0: 
        convertView.setBackgroundColor(Color.RED); 
        break; 
       case 1: 
        convertView.setBackgroundColor(Color.GREY); 
        break; 
       case 2: 
        convertView.setBackgroundColor(Color.GREEN); 
        break; 
       case 3: 
        convertView.setBackgroundColor(Color.YELLOW); 
        break; 
       default: 
        break; 
       } 
    } 

希望它可以帮助...

1

你拿到状态[位置]值是否正确? 您可以编写Log state来检查值或调试代码。

如果您的数值正确无误,请尝试下面的代码片段。

TextView number = (TextView) rowView.findViewById(R.id.tv_number); 

switch(Integer.parseInt(status[position])){ 
case 1: number.setBackgroundColor(Color.parseColor("#FF0000")); 
     break; 

case 2: number.setBackgroundColor(Color.parseColor("#4D4D4D")); 
     break; 

case 3: number.setBackgroundColor(Color.parseColor("#00FF00")); 
     break; 

case 4: number.setBackgroundColor(Color.parseColor("#FFFF00")); 
     break; 

default : 
     break; 
} 

希望得到这个帮助。

相关问题