2013-04-11 92 views
0

我創建一個自定義列表視圖,並創造一個適配器它。我希望能夠在自定義列表視圖中以編程方式更改不同視圖的不同樣式方面。我的自定義列表視圖是一個內部有TextView的RelativeLayout。我嘗試了以下,但沒有運氣。沒有錯誤或任何東西。機器人 - 編程修改列表視圖背景顏色

View myView = adapter.getView(position, null, null); 

myView.setBackgroundColor(Color.parseColor("#93E6CD"));  
myView.findViewById(R.id.layout).setBackgroundColor(Color.parseColor("#93E6CD")); 
adapter.notifyDataSetChanged(); 

這裏是我的自定義列表視圖

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dip" 
    android:id="@+id/layout"> 


    <!-- Title--> 
    <TextView 
     android:id="@+id/primaryTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Loading..." 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="18dip" 
     android:textStyle="bold"/> 

    <!-- Secondary title --> 
    <TextView 
     android:id="@+id/secondaryTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/primaryTitle" 
     android:textColor="#343434" 
     android:textSize="12dip" 
     android:layout_marginTop="1dip" 
     android:text="" /> 

    <!-- Rightend Data --> 
    <TextView 
     android:id="@+id/rightData" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@id/primaryTitle" 
     android:gravity="right" 
     android:text="" 
     android:layout_marginRight="5dip" 
     android:textSize="12dip" 
     android:textColor="#B53021" 
     android:textStyle="bold"/> 

</RelativeLayout> 

回答

0

我們將無法進行,直到您提供更多信息。

我懷疑你沒有正確設置你的ListView。你真的想顯示一個類似項目的列表,比如數據庫中的行嗎?如果是這樣,我希望看到ListView對象的定義,無論是在代碼或XML。我也想看看 適配器的定義。看起來您已經爲ListView定義了一個ListAdapter,但我也想知道支持ListAdapter的是什麼。

myView.findViewById(R.id.layout).setBackgroundColor() 

看起來很可疑。您似乎在尋找myView的子視圖,但我沒有看到您定義此子視圖的位置。

通常情況下,你定義一個ListView爲您的整個UI佈局文件,一個單獨的項目佈局在ListView定義一個項目的一部分,然後適應一個數據源到ListView。

+0

我想有太多的東西要粘貼在這裏。比方說,我沒有使用任何自定義功能,並有一個基本列表,當一個項目被點擊時,它開始一個活動的結果。我在其他活動中做了一些事情,並完成了()使我回到了列表中。我將如何使文本綠色/背景藍色/無論在onActivityResult()方法中的列表項? – David 2013-04-12 13:14:23