我是新來android,我想顯示替代文字顏色和替代背景顏色,但只有背景顏色工作。當我嘗試兩種方式時,我都會收到錯誤信息。
下面是logcat中所示的錯誤:在列表視圖中的替代文字和背景顏色
android.widget.RelativeLayout不能轉換到android.widget.TextView
這是getView()
方法在我的適配器類:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
if(convertView==null)
{
inflater = (LayoutInflater)mcontext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = new ViewHolder();
convertView = inflater.inflate(R.layout.awards_layout_circle, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.Text_View);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.profile_image);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
if (position % 2 == 0){
convertView.setBackgroundResource(R.color.colorNav);
((TextView) convertView).setTextColor(Color.WHITE);
} else {
convertView.setBackgroundResource(R.color.colorWhite);
((TextView) convertView).setTextColor(Color.BLACK);
}
view.txtViewTitle.setText(listAward.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
和awards_layout_circle.xml
使用項目列表:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/bunnyarj"
android:layout_gravity="center"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:id="@+id/Text_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18dp"
android:text="wsdwedwebd dwedbewd w"
android:fontFamily="sans-serif-medium"
android:layout_below="@+id/profile_image"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
((TextView的)convertView).setTextColor(Color.WHITE);在這裏你正在將類型爲Relative佈局的convertView轉換爲給出錯誤的textview –