2013-07-26 94 views
1

我用自定義適配器創建了一個常規列表視圖,一切都很好。它顯示了對象的名稱(正是我想要的)。我也想展示其他一些信息。所以從我的研究中,我發現我需要創建一個自定義文本視圖來處理這個需求。我試圖做一個基本的,我改變背景顏色爲黑色以外的任何東西,但不工作。我真的不知道爲什麼。下面是自定義適配器代碼:安卓在ListView中的自定義TextView

@Override 
public View getView(int arg0, View arg1, ViewGroup arg2) { 
    // TODO Auto-generated method stub 
    if(arg1==null) 
     arg1 = layoutInflator.inflate(R.layout.simplerow ,arg2, false); 
    TextView name = (TextView)arg1.findViewById(android.R.id.text1); 
    name.setText(discountObjectArray[arg0].name); 
    return arg1; 
} 

,這裏是很簡單的自定義的TextView在simplerow.xml代碼:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/customTextView" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="10dp" 
android:textSize="16sp" 
android:textDirection="rtl" 
android:background="@color/thankYouLabelColor"> 
</TextView> 

與於R android.R.layout.simple_list_item_1的簡單變化.layout.simplerow logcat在第49行顯示錯誤:

name.setText(discountObjectArray[arg0].name); 

任何想法爲什麼這不起作用?

+0

'TextView的名稱=( TextView中) arg1.findViewById(R.id.customTextView);'id是'customTextView'。你指的是android框架中的一個。 – Raghunandan

+0

在49行發佈logcat錯誤 – Suji

回答

2

你有這樣的

 android:id="@+id/customTextView" 

替換此

 TextView name = (TextView)arg1.findViewById(android.R.id.text1); 

通過

 TextView name = (TextView)arg1.findViewById(R.id.customTextView); 

這裏的R.id名單從Android包

http://developer.android.com/reference/android/R.id.html

你指的是一個在android包android.R.id.text1,而你在XML ID有textview爲customTextView

看id屬性這裏

http://developer.android.com/guide/topics/ui/declaring-layout.html

+0

不錯的答案:) .. –

1

問題是你需要改變的TextView的id,所以更改:

TextView name = (TextView)arg1.findViewById(android.R.id.text1); 

有:

TextView name = (TextView)arg1.findViewById(R.id.customTextView); 
1

從這個變化android.R.id.text1R.id.customTextView

TextView name = (TextView)arg1.findViewById(android.R.id.text1); 

這是...

TextView name = (TextView)arg1.findViewById(R.id.customTextView);