2016-01-27 32 views
0

我需要更改listview backgroundcolor屬性的顏色。我可以做到這一點,我改變了顏色,但它改變了我的所有行相同的顏色。我的意思是,我需要一個行用紅色,其他用綠色......我把下面的代碼,以幫助:在不改變其他列表視圖的情況下更改一個textview的顏色

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
      { 
       @Override 
       public View getView(int position, View convertView, ViewGroup parent) 
       { 

        View view = super.getView(position, convertView, parent); 
        TextView text = (TextView) view.findViewById(android.R.id.text1); 

       for(int i = 0; i < colores.size(); i++) 
       { 
        if(colores.get(i).equals("0")) 
        { 
         text.setBackgroundColor(Color.GREEN); 
        } 
        else if(colores.get(i).equals("1")) 
        { 
         text.setBackgroundColor(Color.RED); 
        } 
        else if(colores.get(i).equals("2")) 
        { 
         text.setBackgroundColor(Color.YELLOW); 
        } 
        else 
        { 
         text.setBackgroundColor(Color.WHITE); 
        } 
       } 

       return view; 
       } 
      }; 

在第一時間,我有「colores.get(I)= 1」 ,所以它將顏色更改爲紅色,但後來我有「colores.get(i)= 2」,所以它將顏色更改爲黃色。但我只需要將第二行改爲黃色,而不是第一行,第一行必須是紅色。例如,當「i = 0」時,我將第0行更改爲該顏色,但當「i = 1」時,我需要更改所有顏色列表,我想只更改第1行,而不是所有的行。

任何人都可以幫助我嗎?謝謝!

回答

0

,我認爲你應該在考慮給功能getView「位置」參數。我假設你喜歡有第一排:綠色,下一個紅色,下一個黃色,其餘所有白色。要做到這一點:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
     { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 

       View view = super.getView(position, convertView, parent); 
       TextView text = (TextView) view.findViewById(android.R.id.text1); 


       if(colores.get(position).equals("0")) 
       { 
        text.setBackgroundColor(Color.GREEN); 
       } 
       else if(colores.get(position).equals("1")) 
       { 
        text.setBackgroundColor(Color.RED); 
       } 
       else if(colores.get(position).equals("2")) 
       { 
        text.setBackgroundColor(Color.YELLOW); 
       } 
       else 
       { 
        text.setBackgroundColor(Color.WHITE); 
       } 


      return view; 
      } 
     }; 

在這種方法中,每行(poistion是索引)將被測試的顏色。

這裏沒有必要通過顏色數組迭代迭代,因爲「位置」索引應該等於來自顏色數組的顏色索引。

+1

它很完美!非常感謝!!!! :) – Imrik

+0

沒問題。我很高興能夠提供幫助。 – MadOX

0

試試這個:

if (position % 2 == 1) { 
    text.setBackgroundColor(Color.RED); 
} else { 
    text.setBackgroundColor(Color.YELLOW); 
} 

此代碼設置背景顏色或奇數行至黃色甚至紅色

0

刪除該循環,並獲得列表視圖項目位置的顏色。您只需在每個行視圖中設置顏色。 像下面這樣的變化,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, configureList(idRuta, rocodromo, dificultad)) 
     { 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 

       View view = super.getView(position, convertView, parent); 
       TextView text = (TextView) view.findViewById(android.R.id.text1); 

       if(colores.get(position).equals("0")) 
       { 
        text.setBackgroundColor(Color.GREEN); 
       } 
       else if(colores.get(position).equals("1")) 
       { 
        text.setBackgroundColor(Color.RED); 
       } 
       else if(colores.get(position).equals("2")) 
       { 
        text.setBackgroundColor(Color.YELLOW); 
       } 
       else 
       { 
        text.setBackgroundColor(Color.WHITE); 
       } 

      return view; 
      } 
     }; 
相關問題