1

我已經使用SimpleCursorAdapter列出了數據庫中的名稱,我想使用SimpleCursorAdapter.ViewBinder的方法更改特定名稱的顏色。我在運行此方法時遇到問題,我的數據庫包含不同的名稱,但ListView將顯示所有namse作爲一個特定名稱。我究竟做錯了什麼?如何更改特定名稱的文本顏色?是否有可能使用ViewBinder使用SimpleCursorAdapter.ViewBinder更改Listview中的文本顏色

這是ViewBinder我的部分代碼:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() { 

    @Override 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
     // TODO Auto-generated method stub 
     String[] temp = dh.return_result(); 
     tv = (TextView)findViewById(R.id.textView1); 
     tv = (TextView) view; 
     for(int i = 0; i<temp.length; i++) 
     { 
      if(temp[i].equalsIgnoreCase("Ameer Zhann")) 
      { 
       tv.setText(temp[i]); 
       tv.setTextColor(Color.rgb(58, 58, 224)); 
       return true; 
      } 
     } 
     return false; 
    } 
}; 

,這是我的輸出圖像:

Image

我該如何解決這個問題?

回答

5

試試這個方法:

public boolean setViewValue(View view, Cursor cursor, int columnIndex){  
    int getIndex = cursor.getColumnIndex("Name"); 
    String empname = cursor.getString(getIndex); 
    tv = (TextView) view; 
    tv.setTextColor(Color.WHITE); 
    tv.setText(empname); 
    if(empname.equals("Any String")) 
    {     
     tv.setTextColor(Color.rgb(58, 58, 224)); 
     return true; 
    } 
    return false;   
} 
+1

+1 – MKJParekh 2011-12-15 10:05:33

1

嘗試與其他部分

if(temp[i].equalsIgnoreCase("Ameer Zhann")){ 
    tv.setText(temp[i]); 
    tv.setTextColor(Color.rgb(58, 58, 224)); 
}else{ 
    tv.setText(temp[i]); 
} 

,並在年底迴歸真實,而不是虛假

+0

是的,這else部分也很重要。謝謝 – Praveenkumar 2011-12-15 10:03:02

3

代碼你問到底是什麼 - 在Cursor每個元素你經歷所有的列表和設置的文本每個元素。我認爲「Ameer Zhann」是您的列表中的最後一個結果,因此只有TextView中的文字留下。

方法setViewValue(...)調用Cursor的每個元素。所以,你不需要任何循環,只需用光標值tv.setText(Cursor.getString(...));填充文本。

也有一些奇怪的事情,此代碼:

tv = (TextView)findViewById(R.id.textView1); 
tv = (TextView) view; 

view自帶的參數:param - 已經查看與ID R.id.textView1 - 所以只是刪除的findViewById調用。

+0

如果我刪除findViewById的意思,它會影響listview而不是textview。所以,對於正確的解決方案,這是重要的 – Praveenkumar 2011-12-15 10:05:01