2013-02-10 83 views
1

我有一個列表視圖,並希望根據從SQLite數據庫檢索到的某些條件更改視圖(@+id/panel)顏色。使用SimpleCursorAdapter.ViewBinder更改視圖的顏色

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="10dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.01" 
     android:orientation="vertical" > 

     <View 
     android:id="@+id/panel" 
     android:layout_width="10dip" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:background="#ffa500" 
     android:layout_marginRight="4dip" 
     /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="2.86" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textViewB" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:text="" /> 
    </LinearLayout> 
</LinearLayout> 

我使用SimpleCursorAdapter.ViewBinder做到這一點:

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

@Override 
public boolean setViewValue(View view, Cursor cursor, int columnIndex){  

    if (view.getId() == R.id.panel) 
    {        
     String name = cursor.getString(1); 

     if (name.equals("Ravi")) { 
      view.setBackgroundColor(Color.RED); 
     } else { 
      view.setBackgroundColor(Color.BLUE); 
     } 
     return true; 
    } 
    return false; 
}    

listView.setAdapter(dataAdapter); 
dataAdapter.setViewBinder(binder); 

此代碼仍然無法正常工作。代碼從未通過條件。 我可以更改TextView對象(@+id/textViewB)的字體顏色,但從未成功更改「查看」顏色。

感謝您的回答。

+0

是否只有一行,你想與其他顏色不同?還是幾行?如果只有一行,我可能會有所幫助。 – 2013-02-10 21:57:53

+0

幾行取決於條件。但我希望看到你的解決方案也行 – neonerdy 2013-02-10 23:00:26

+0

爲什麼不擴展SimpleCursorAdapter並重寫getView。 – 2013-02-10 23:45:37

回答

0

SimpleCursorAdapter

一個簡單的適配器從光標到TextViews或在XML文件中定義 ImageViews映射列。

我不知道什麼View你正試圖將值從數據庫綁定,但我認爲它僅適用於該TextView對象(@+id/textViewB)因爲TextViewView類型之一,你可以使用此接口。

請嘗試使用SimpleAdapter.ViewBinder來代替?否則使用getView()可能會給你很好的結果。