0

I'm using this library for circular images in my android project.

This is my adapter class

public class SingleAdapter extends ArrayAdapter<SingleItem> 
 {
    Context context;

    public SingleAdapter(Context context, int resourceId,
            List<SingleItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        CircularImageView imageView;
        TextView txtTitle;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        SingleItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_single_json, null);
            holder = new ViewHolder();

            holder.txtTitle = (TextView) convertView.findViewById(R.id.txt);
            holder.imageView = (CircularImageView) convertView.findViewById(R.id.img);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtTitle.setBackgroundResource(R.drawable.json_bubble); 
        holder.txtTitle.setText(rowItem.getMessage());


        holder.imageView.setBackgroundResource(rowItem.getImageId());


        return convertView;
    }
}

But on running,this gives an exception like java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.mikhaellopez.circularimageview.CircularImageView

Somebody please help me to fix this...

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.sample"
    android:id="@+id/chat_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2a2a34"
    android:padding="3dp"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <RelativeLayout
            android:id="@+id/icon_layout"
            android:layout_width="match_parent"
            android:layout_weight="4"
            android:layout_height="match_parent" >


            <com.mikhaellopez.circularimageview.CircularImageView
        android:id="@+id/img1"
        android:layout_width="55dp"
        android:layout_height="55dp"
        app:border="true"
        app:border_color="#01c7f4"
        app:border_width="4dp"
         />
            </RelativeLayout>

        <RelativeLayout
            android:id="@+id/text_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:layout_weight="1" >



                 <TextView
                     android:id="@+id/txt"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentLeft="true"
                     android:layout_alignParentTop="true"
                     android:text="Message"
                     android:textAppearance="?android:attr/textAppearanceSmall"
                     android:textColor="#ffffff" />

            </RelativeLayout>


    </LinearLayout>

</LinearLayout>
Nevaeh
  • 1,519
  • 7
  • 24
  • 45

2 Answers2

2

You have provided wrong id in your code to refer your CircularImageView as R.id.img whereas its android:id="@+id/img1" in your layout file.

change your below line

 holder.imageView = (CircularImageView) convertView.findViewById(R.id.img);

to as below

 holder.imageView = (CircularImageView) convertView.findViewById(R.id.img1);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Please post your full logcat error in your question @Nevaeh And also once try to clean your project. – GrIsHu Aug 29 '14 at 11:03
  • Thanks this worked for me. @GrIsHu please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:24
1

You are refering to img here:

convertView.findViewById(R.id.img);

But in your layout you define it as img1 here:

android:id="@+id/img1"

Make them the same one way or the other

Dreagen
  • 1,733
  • 16
  • 21
  • Ohhh.It was such a silly mistake !!I didn't notice that.Thank you so much buddy :) There was one more correction.i changed holder.imageView.setBackgroundResource(rowItem.getImageId()) to holder.imageView.setImageResource(rowItem.getImageId()) – Nevaeh Aug 29 '14 at 11:33