2013-07-26 103 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);