vishnu
vishnu

Reputation: 87

Alternate text and background color in listview

I am new to android, I am trying to display alternate text color and alternate background color, but only background color is working. When I try both, I'm getting error.
Here's the error shown in LogCat:

android.widget.RelativeLayout cannot be cast to android.widget.TextView

This is getView() method in my adapter class:

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

And awards_layout_circle.xml using to item list:

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

Upvotes: 0

Views: 426

Answers (2)

Blackbelt
Blackbelt

Reputation: 157487

you are using the ViewHolder. No reason to cast the convertView to any type of object (expecially to the wrong one). Just use

view.txtViewTitle.setTextColor()

in your if/else

Upvotes: 1

Sahil Garg
Sahil Garg

Reputation: 262

You have change the code like this,

if (position % 2 == 0){
    convertView.setBackgroundResource(R.color.colorNav);
    ((TextView) view.txtViewTitle).setTextColor(Color.WHITE);
} else {
    convertView.setBackgroundResource(R.color.colorWhite);
    ((TextView) view.txtViewTitle).setTextColor(Color.BLACK);

}

Upvotes: 0

Related Questions