使用CircularImageView Library显示图像

时间:2014-08-29 10:52:59

标签: android imageview

我在我的android项目中使用this库来获取圆形图像。

这是我的适配器类

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

但是在运行时,这会产生一个例外,例如java.lang.ClassCastException: android.widget.ImageView无法转换为com.mikhaellopez.circularimageview.CircularImageView

有人请帮我解决这个问题......

这是我的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>

2 个答案:

答案 0 :(得分:2)

您在代码中提供了错误的ID,以便将CircularImageView引用为R.id.img,而将android:id="@+id/img1"引用到您的布局文件中。

更改以下行

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

如下

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

答案 1 :(得分:1)

您指的是img

convertView.findViewById(R.id.img);

但在您的布局中,您可以在此处将其定义为img1

android:id="@+id/img1"

使它们以相同的方式使用