1

我使用自定义适配器创建了一个常规列表视图,一切都很好。它显示了对象的名称(正是我想要的)。我还想展示一些其他信息。所以从我的研究中,我发现我需要创建一个自定义文本视图来处理需求。我尝试做一个基本的,将背景颜色更改为黑色以外的任何颜色,但不起作用。我真的不知道为什么。这是自定义适配器的代码:

@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;
}

这是 simplerow.xml 中用于简单自定义 textview 的代码:

<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>

通过将 android.R.layout.simple_list_item_1 简单更改为 R.layout.simplerow,logcat 在第 49 行显示错误,即:

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

知道为什么这不起作用吗?

4

3 回答 3

2

你有这个

      android:id="@+id/customTextView"   

替换这个

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

经过

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

这是来自 android 包的 R.id 列表

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

您指的是 android 包中的那个,android.R.id.text1而您在 textview 的 xml id 中具有customTextView.

看这里的id属性

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

于 2013-07-26T11:38:39.927 回答
1

从这个变化android.R.id.text1R.id.customTextView

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

那是...

TextView name = (TextView)arg1.findViewById(R.id.customTextView);
于 2013-07-26T11:39:26.310 回答
1

问题是你需要改变Textview的id,所以改变:

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

和:

TextView name = (TextView)arg1.findViewById(R.id.customTextView);
于 2013-07-26T11:37:04.187 回答