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)
的字體顏色,但從未成功更改「查看」顏色。
感謝您的回答。
是否只有一行,你想與其他顏色不同?還是幾行?如果只有一行,我可能會有所幫助。 – 2013-02-10 21:57:53
幾行取決於條件。但我希望看到你的解決方案也行 – neonerdy 2013-02-10 23:00:26
爲什麼不擴展SimpleCursorAdapter並重寫getView。 – 2013-02-10 23:45:37