2017-09-27 62 views
0

我爲Change the color of a specified item in a listview for android同樣的問題,通過Kartheek(謝謝)回答適用於測試如下:的Android變化TextView的字體顏色在ListView一個條件

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){ 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
{ 
      View view1 = super.getView(position, convertView, parent); 
//   if (position % 2 == 0) { //Place the condition where you want 
to change the item color. 
     testo = messaggi.get(position); 
      if(testo.substring(0,5).equals("27-09")){ 
      view1.setBackgroundColor(Color.parseColor("#e0e0ef")); 
     } else { 
      //Setting to default color. 
      view1.setBackgroundColor(Color.WHITE); 
     } 
     return view1; 
     } 
    }; 

問題:我而改變字體顏色但view1.setTextColor(Color.parseColor(「#E0E0EF」);似乎沒有工作;

+0

寫convertView更換.setBackgroundColor(Color.parseColor( 「#e0e0ef」)); – Ankita

+0

包含您的db_msg佈局xml代碼 – SiSa

+0

向我們展示此佈局R.layout.db_msg –

回答

0

顯示,美國db_msg這種佈局在佈局有一個TextView中剛剛獲得的這個名字與「tvIDFrom_db_msg_layout」這

adapter = new ArrayAdapter<String>(this,R.layout.db_msg,messaggi){ 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
    { 
      View view1 = super.getView(position, convertView, parent); 
     if (position % 2 == 0) { //Place the condition where you want to change the item color. 
      testo = messaggi.get(position); 
       TextView tvText = (TextView) view1.findViewById(R.id.tvIDFrom_db_msg_layout); 
      if(testo.substring(0,5).equals("27-09")){ 

       tvText.setTextColor(Color.parseColor("#yourHexCode")); 
      } else { 
       //Setting to default color. 
       tvText.setTextColor(Color.WHITE); 
      } 
     return view1; 
     } 
    }; 
+0

是的,它的作品!我能使用tvText.setTextColor(Color.parseColor(」 #0000ef「)); – alberto

0

看起來像你錯過了最後關閉')'在你的命令。否則接縫正確:

view1.setTextColor(Color.parseColor("#E0E0EF")); 
0
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view1; 
view1=convertView; 
if (convertView == null) { 
     view1 = inflater.inflate(R.layout.db_msg, null); 
     testo = messaggi.get(position); 
     if(testo.substring(0,5).equals("27-09")){ 
     view1.setBackgroundColor(Color.parseColor("#e0e0ef")); 
    } 
else { 
     //Setting to default color. 
     view1.setBackgroundColor(Color.WHITE); 
    } 
     return convertView; 
}