2011-12-12 35 views
2

我有一個列表視圖,列出了我的Android應用程序中的作業。我想根據他們的工作類型對行進行着色。以下是我用來設置Listview的代碼。分別在列表視圖中設置單獨行的背景顏色

startManagingCursor(cursor); 
    adapter = new SimpleCursorAdapter(this, R.layout.menu_item, cursor, FROM, TO); 
    menuList.setAdapter(adapter); 

以下是Listview項目的Xml佈局。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="10sp"> 
    <TextView 
     android:id="@+id/rowid" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/exhibitor_header" /> 
    <TextView 
     android:id="@+id/rowidtwo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/listview_background" 
     android:layout_below="@+id/rowid"/> 
</RelativeLayout> 

如何設置列表視圖以與其作業類別相關的顏色顯示每一行。我可以不使用自定義適配器嗎?

+3

我想你不能沒有自定義適配器... 你必須編寫自己的SimpleCursorAdapter ... – krackmoe

+0

使用自定義適配器.. – user370305

回答

1

以下是您的示例:Android – Applying Alternate Row Color in ListView with SimpleAdapter

定義與上述示例相同的自定義適配器,並且必須在getView()方法內部設置條件來檢查作業類型,並基於該方法可以應用不同的背景色。

+0

謝謝你的例子。我已經使用以下代碼添加了一個自定義適配器public Lectures_Adapter(Context context,List > items,int resource,String [] from,int [] to){super(context,items,resource , 從到); this.data = items; }當我嘗試調用它時,當我嘗試修改示例以使用我的數據時出現錯誤。我的數據來自Sqlite表並加載到遊標中。該錯誤要求我通過將其轉換爲List > – user616076

+1

將其更改,我的適配器擴展SimpleAdapter而不是SimpleCursorAdapter,但您的解決方案適用於我。謝謝 – user616076

相關問題