Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:22:36+00:00 2026-05-28T18:22:36+00:00

I am trying to create a custom listview in android by extending ListView. The

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:22:37+00:00Added an answer on May 28, 2026 at 6:22 pm

    Error in the trace explains the problem clearly.
    You are doing
    holder.iView.setLayoutParams(new ListView.LayoutParams.... );
    But its a child view of the LinearLayout.
    So, you need to do
    holder.iView.setLayoutParams(new LinearLayout.LayoutParams.... );
    and things will go fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom ListView layout similar to android.R.layout.simple_list_item_2. However, when I
I'm trying to create custom listview in android. When I try to access my
I'm trying to create a custom control - a button - which will have
I am trying to define a composite list item in an Android ListView but
I am trying to create a custom ListView using an ArrayAdapter . POJO Item
I have a Listview with custom cursor adapter. I'm trying to prevent input in
What I've done Hello Guys, I'm trying to create a custom ListView layout. I've
I'm trying to create a custom view (one that is not 100% listview, for
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom JSP tag that would take an array object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.