【发布时间】:2014-05-31 14:28:32
【问题描述】:
我希望listview 的每一项都有不同的背景颜色。以下是我的自定义适配器getView() 方法的代码。
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
View v = super.getView(position, convertView, parent);
RatingBar ratingBar = ((RatingBar)convertView.findViewById(R.id.my_choices_row_rating));
ratingBar.setRating(mRatings[position]);
TextView tv = (TextView)convertView.findViewById(R.id.my_choices_row_text);
tv.setText(mStrings[position]);
Log.v(TAG,"getView nalist=" + mNAList[position]);
Log.v(TAG,"getView gotlist=" + mGotList[position]);
if(mNAList[position].equalsIgnoreCase("N/A")){
TextView tv1 = (TextView)convertView.findViewById(R.id.my_choices_row_na_text);
tv1.setText(mNAList[position]);
}
if(mGotList[position].equalsIgnoreCase("Purchased")){
TextView tv2 = (TextView)convertView.findViewById(R.id.my_choices_row_got_text);
tv2.setText(mGotList[position]);
}
convertView.setBackgroundColor(mColors[position]);
return convertView;
}
}
这是我的列表行的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/RHE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="5dp"
android:layout_span = "3">
<TableRow android:layout_height="wrap_content">
<RatingBar
android:id="@+id/my_choices_row_rating"
android:progressDrawable="@drawable/hearts_rating_bar"
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:layout_height="25sp"
android:numStars="5"
android:stepSize="1" />
<TextView
android:id="@+id/my_choices_row_text"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:textSize="18sp"
android:gravity="left|center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<CheckBox
android:id="@+id/my_choices_row_checkBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</TableRow>
<TableRow android:layout_height="wrap_content">
<TextView
android:id="@+id/my_choices_row_na_text"
android:textSize="12sp"
android:gravity="left|center"
android:textColor="#8B0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/my_choices_row_got_text"
android:textSize="12sp"
android:textColor="#8B0000"
android:gravity="left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
我不断收到以下错误
06-01 00:20:52.057: E/AndroidRuntime(711):FATAL EXCEPTION:main
06-01 00:20:52.057: E/AndroidRuntime(711):java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
06-01 00:20:52.057: E/AndroidRuntime(711):at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
【问题讨论】:
-
检查您的 mViewResourceId。你确定它是 TextView 吗?
-
问题与构造函数有关。超级期望布局包含一个文本视图,而您正在为它提供其他内容。
-
我已经编辑了我的问题并添加了我的布局代码。
-
尝试在行布局中添加一个文本视图。你不需要为这个文本视图做任何事情。
-
tana@ 我的行布局中已经有 3 个文本视图。
标签: android listview android-arrayadapter custom-adapter