Android extendiendo listview y baseadapter sin usar ListActivity

I am trying to create a custom listview in android by extending ListView. The custom Listview needs to have multiple columns. I am extending BaseAdapter and am inflating a custom row in getView() but am getting errors in the onMeasure() of my extended Listview.

    public class ImageListView  extends ListView{
    private boolean isFirstMeasure;
    private Paint paint=new Paint();
    private NoteActivity pActivity;
    private Cursor cursor;
    private int columnIndex;
    private int parentWidth;
    private int parentHeight;
    private int itemWidth;
    private boolean isImageSelected;
    private int indexSelected;
    private Bitmap selectedBitMap;

    public Bitmap getSelectedBitMap() {
        return selectedBitMap;
    }

    public void setSelectedBitMap(Bitmap selectedBitMap) {
        this.selectedBitMap = selectedBitMap;
    }

    public int getIndexSelected(){
        return indexSelected;
    }

    public void setIndexSelected(int indexSelected) {
        this.indexSelected = indexSelected;
    }

    static class ViewHolder 
    {
      ImageView iView;
      CheckBox cbListCheck ;
      ImageDraggerView dragView;
    }

    private class ImageAdapter extends BaseAdapter{
        private Context mContext;

        public ImageAdapter(Context localContext) {
            super();
            mContext = localContext;

        }

        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = new ViewHolder();
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = (LinearLayout)inflater.inflate(R.layout.imagelistrow, null);
                holder.iView   = (ImageView)convertView.findViewById(R.id.imgBitmap);
                holder.cbListCheck   = (CheckBox)convertView.findViewById(R.id.multiplechecker);
                holder.dragView  = (ImageDraggerView)convertView.findViewById(R.id.singledragger);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }

           holder.cbListCheck.setChecked(false);

           //ImageView iView= new ImageView(mContext,null);
           cursor.moveToPosition(position);
           int imageID = cursor.getInt(columnIndex);
           Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
           String url = uri.toString();
           int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
           Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(pActivity.getContentResolver(),originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
           holder.iView.setImageBitmap(b);
           holder.iView.setLayoutParams(new ListView.LayoutParams(parentWidth/5, itemWidth));
           holder.iView.setScaleType(ImageView.ScaleType.FIT_XY);
           //return holder.iView;
           return convertView;  

        }


    }  

    public ImageListView(Context context) {
        super(context);
        pActivity=(NoteActivity)context;
        initLayout();
    }

    public ImageListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        pActivity=(NoteActivity)context;
        initLayout();
    }

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
       if(isFirstMeasure){
           super.onMeasure(widthMeasureSpec, heightMeasureSpec);    
           parentWidth = MeasureSpec.getSize(widthMeasureSpec);
           parentHeight = MeasureSpec.getSize(heightMeasureSpec);
           itemWidth=parentHeight/12;
           isFirstMeasure=false;
       }
       this.setMeasuredDimension(parentWidth/5,parentHeight/4);

    }

    public void initLayout(){
        isFirstMeasure=true;
        paint.setAntiAlias(true);
        paint.setColor(Color.argb(128, 256, 256, 256));
        paint.setStyle(Paint.Style.STROKE);
        paint.setDither(true);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        this.setWillNotDraw(false);
        isImageSelected=false;
        String[] projection = {MediaStore.Images.Media._ID};
        cursor = pActivity.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection, 
                 MediaStore.Images.Media.DATA + " like ? ", new String[] {"%Images%"},null);
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
        this.setAdapter(new ImageAdapter(pActivity));
    }
   }

following is the row which i am trying to inflate.

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <CheckBox android:id="@+id/multiplechecker"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:checked="false"/>
    <ImageView android:id="@+id/imgBitmap"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"/>
    <com.ui.ImageDraggerView android:id="@+id/singledragger"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true">
    </com.ui.ImageDraggerView>    
</LinearLayout

>

but i am getting the following error.

E/AndroidRuntime( 775): FATAL EXCEPTION: main E/AndroidRuntime( 775): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams E/AndroidRuntime( 775): at android.widget.LinearLayout.measureVertical(LinearLayout.java:587) E/AndroidRuntime( 775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) E/AndroidRuntime( 775): at android.view.View.measure(View.java:10577) E/AndroidRuntime( 775): at android.widget.ListView.measureScrapChild(ListView.java:1165) E/AndroidRuntime( 775): at android.widget.ListView.measureHeightOfChildren(ListView.java:1230) E/AndroidRuntime( 775): at android.widget.ListView.onMeasure(ListView.java:1139) E/AndroidRuntime( 775): at com.ui.ImageListView.onMeasure(ImageListView.java:139)

preguntado el 31 de enero de 12 a las 08:01

1 Respuestas

Error in the trace explains the problem clearly.
Tú lo estás haciendo
holder.iView.setLayoutParams(new ListView.LayoutParams.... );
But its a child view of the LinearLayout.
Entonces, necesitas hacer
holder.iView.setLayoutParams(new LinearLayout.LayoutParams.... );
and things will go fine.

Respondido el 31 de enero de 12 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.