2012-07-06 138 views
0

我有這樣的代碼在我的ArrayAdapter:ListView控件設置背景顏色

public View getView(int position, View convertView, ViewGroup parent) { 
    View row = super.getView(position, convertView, parent); 

    Toast.makeText(getApplicationContext(), getItem(position).getString("text") + " = " + getItem(position).getString("my"), Toast.LENGTH_SHORT).show(); 

    if(getItem(position).getString("my") == "0") { 
     row.setBackgroundColor(getResources().getColor(R.color.ekvi)); 
    } 
    TextView tv = (TextView) row.findViewById(android.R.id.text1); 
    tv.setText(getItem(position).getString("text")); 
    tv.setTextColor(Color.BLACK); 

    return row; 
} 

雖然背景顏色不適用。我查了一下「我」的字符串,它是真正的有時是「0」,所以它必須工作。

我在做什麼錯?

+0

你已經嘗試使用'如果(的getItem(位置).getString( 「我」)等於( 「0」)'? – 2012-07-06 12:25:16

+0

嘗試使用equalsIgnoreCase()。比較包含數字的字符串時 – 2012-07-06 12:28:10

+0

equalsIgnoreCase沒有任何區別:P – Carnal 2012-07-06 12:33:16

回答

2

使用.equals() to compare Strings objects.

  • == compares Strings Refrences(memory location)

  • .equals() compares Strings characters(value)

if(getItem(position).getString("my").equals("0")) { 
+0

謝謝!我真的忘了這一點(當時編程C#的地方==也適用)。 – Uhehesh 2012-07-06 12:44:33