Skip to content Skip to sidebar Skip to footer

Switch Between Gallery And A LinearLayout - ClassCastException

Thanks for reading! I am building a custom Gallery app where the first thumbnail is an album cover displaying album details. Here's the flow: getView() { //inflate cover.xml whi

Solution 1:

Here's what it looks like to me. When position == 0 you are returning convertView, which is a View. When you return "else", you are returning a ImageView. Your method is set to return a View. Try casting your ImageView to a View before returning it.

Try: return (View) imgView;

Never tried it myself though...


Solution 2:

Add this imageview into your layout xml, and then retrieve it from convertview and at the end return the convert view. This may solve the problem. I have worked a lot on Gallery widget, if there is more problem do let me know.


Solution 3:

After trying all the suggestions given by helpful people here,I still wasn't able to get across the ClassCastException.

So, as a workaround - I sort of "overlayed" the Gallery with other views that I wanted to enable/disable.

This is a workaround, so if someone comes up with a better answer - do post it here so I can accept it.

So here's what worked for me:

public View getView(int position, View convertView, ViewGroup parent) {
            //TODO: Recycle view
            //onvertView = mInflater.inflate(R.layout.cover, null);
            //ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);
            ImageView imgView = new ImageView(mContext);
            imgView.setImageResource(mImageIds[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(mGalleryItemBackground);

            if(position == 0) {
                tvText1.setText("AlbumText1");
                tvText2.setText("AlbumText2");
                tvText3.setVisibility(View.VISIBLE);
                bottomBar.setVisibility(View.VISIBLE);
            }
            else {
                tvText1.setText("ImageText1"); 
                tvText2.setText("ImageText2");
                tvText3.setVisibility(View.GONE);
                bottomBar.setVisibility(View.GONE);
            }
            return imgView;
        }

Here's my layout main.xml file:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Gallery android:id="@+main/gallery" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <!-- <ImageView android:id="@+main/imgImage" -->
    <!-- android:layout_width="fill_parent" android:layout_height="fill_parent" -->
    <!-- android:adjustViewBounds="true"> -->
    <!-- </ImageView> -->
    <TextView android:id="@+main/tvText2" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:singleLine="true"
        android:maxLines="1" android:text="Text2"
        android:layout_alignParentBottom="true" />
    <TextView android:id="@+main/tvText1" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:maxLines="2"
        android:text="Text1" android:layout_above="@main/tvText2" />
        <RelativeLayout android:id="@+main/bottomBar" android:layout_alignParentBottom="true"
            android:layout_width="fill_parent" android:layout_height="40dip"
            android:background="#A3A1A1">
            <TextView android:id="@+main/tvBottomText" android:layout_height="wrap_content" android:layout_width="fill_parent" 
                android:text="BottomBarText"/>
        </RelativeLayout>
</RelativeLayout>

The rest of the code in Main.java (whose getView method I modified) is almost verbatim from here

Thanks again for helping out!


Post a Comment for "Switch Between Gallery And A LinearLayout - ClassCastException"