Switch between Gallery and a LinearLayout - ClassCastException

流过昼夜 提交于 2019-12-11 23:39:43

问题


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 which includes two textviews and an imageview.
    if(position == 0)
         //set some album-specific text
    else 
         //set image-specific text
}

Here's the actual getView() code:


 public View getView(int position, View convertView, ViewGroup parent) {
            //TODO: Recycle view
            convertView = mInflater.inflate(R.layout.cover, null);
            TextView tvTxt1 = (TextView)convertView.findViewById(R.cover.tvCoverText1);
            TextView tvTxt2 = (TextView)convertView.findViewById(R.cover.tvCoverText2);
            //ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);

            if(position == 0) {
                tvTxt1.setText("AlbumText1");
                tvTxt2.setText("AlbumText2");
                return convertView;
            }
            else {
                tvTxt1.setText("ImageText1"); 
                tvTxt2.setText("ImageText2");
                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);
                return imgView;
                //return convertView;
            }
        }

The cover.xml contains an ImageView and two TextViews.

when I return convertView in the else block, I get a ClassCastException. I am certainly doing something wrong.

I have spent almost two days on this now :(

Please help!


回答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...




回答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.




回答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!



来源:https://stackoverflow.com/questions/5670488/switch-between-gallery-and-a-linearlayout-classcastexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!