2014-04-16 74 views
0

在我的顯示適配器ptrelevant中,我從數據庫中可視化數據。我希望列的相對於TextView的M值變爲紅色。我怎樣才能做到這一點?如何根據字符串值分配顏色

public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> ore,ArrayList<String> turno) { 
     this.mContext = c; 

     this.id = id; 
     this.turnoName = turno; 
     this.oreName = ore; 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return id.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public View getView(int pos, View child, ViewGroup parent) { 
     Holder mHolder; 
     LayoutInflater layoutInflater; 
     if (child == null) { 
     layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     child = layoutInflater.inflate(R.layout.row, null); 
     mHolder = new Holder(); 
     mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno); 
     mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore); 
     mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id); 
     child.setTag(mHolder); 
     } else { 
     mHolder = (Holder) child.getTag(); 
     } 
     mHolder.txt_turno.setText(turnoName.get(pos)); 
     mHolder.txt_ore.setText(oreName.get(pos)); 

     return child; 
    } 

    public class Holder { 
     TextView txt_turno; 
     TextView txt_ore; 
     TextView txt_id; 
    } 
} 
+2

你的問題不清楚 –

+0

我希望列移位中的M值是紅色的,比其他值 – user3461181

+0

'我希望列的相對於TextView的M值轉彎,是紅色的 - - 它是什麼意思? –

回答

0

奇怪...這對我的作品: 試試這個:

 mHolder.txt_turno.setText(turnoName.get(pos)); 
    mHolder.txt_ore.setText(oreName.get(pos)); 
    mHolder.txt_id.setText(dataName.get(pos)); 

    String Turno = turnoName.get(pos); 

mHolder.txt_turno.setTextColor(Color.WHITE); 

if (Turno != null){ 
if (TextUtils.equals(Turno,"M")) 
      mHolder.txt_turno.setTextColor(Color.RED); 

if (TextUtils.equals(Turno,"F")) 
     mHolder.txt_turno.setTextColor(Color.YELLOW); 
} 
return child; 

}

希望幫助您

+0

在列移位有不同的值,所以如果M和M的值必須用紅色氯化,如果另一個值應該保持白色 – user3461181

+0

我試過了你的建議,但它不起作用,於是我試了這個: – user3461181

+0

感謝它現在的作品, – user3461181

0

嘗試增加以HTML格式的文本。我很確定這會改變顏色。您的代碼應該看起來像:

public View getView(int pos, View child, ViewGroup parent) { 
    Holder mHolder; 
    LayoutInflater layoutInflater; 
    if (child == null) { 
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    child = layoutInflater.inflate(R.layout.row, null); 
    mHolder = new Holder(); 
    mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno); 
    mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore); 
    mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id); 
    child.setTag(mHolder); 
    } else { 
    mHolder = (Holder) child.getTag(); 
    } 

    if (turnoName.get(pos).toString().equalsIgnoreCase("M")){ 
    mHolder.txt_turno.setText(Html.fromHtml("<font color='#FF0000'>"+turnoName.get(pos)+"</font>")); 
    }else if(turnoName.get(pos).toString().equalsIgnoreCase("F")){ 
    mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFF00'>"+turnoName.get(pos)+"</font>")); 
    }else{ 
     mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFFFF'>"+turnoName.get(pos)+"</font>")); 
    } 
    mHolder.txt_ore.setText(oreName.get(pos)); 

    return child; 
} 

更改您的getView與上面張貼的代碼。

希望它有幫助!