2014-09-29 49 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; 
} 

希望得到這個幫助。

相關問題