在Gallery和LinearLayout之间切换-ClassCastException

| 谢谢阅读! 我正在构建一个自定义Gallery应用程序,其中第一个缩略图是显示专辑详细信息的专辑封面。流程如下:
getView() {
//inflate cover.xml which includes two textviews and an imageview.
    if(position == 0)
         //set some album-specific text
    else 
         //set image-specific text
}
这是实际的getView()代码:
 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;
            }
        }
cover.xml
包含an3ѭ和两个
TextView
。 当我在
else
块中返回
convertView
时,我得到了ClassCastException。我当然做错了。 我现在已经花了将近两天时间:( 请帮忙!     
已邀请:
这就是我的样子。当position == 0时,您将返回convertView,它是一个View。当您返回\“ else \”时,您将返回ImageView。您的方法设置为返回View。返回之前,请尝试将ImageView投射到View。 尝试:return(View)imgView; 虽然我自己从未尝试过...     
将此imageview添加到布局xml中,然后从convertview检索它,最后返回convert视图。 这样可以解决问题。 我在Gallery小部件上做了很多工作,如果还有其他问题,请告诉我。     

bab

在尝试了乐于助人的人的所有建议之后,我仍然无法理解ClassCastException。 因此,作为一种解决方法-我将ѭ7与“我想启用/禁用的其他视图”“叠加”在一起。 这是一种解决方法,因此,如果有人提出更好的答案-请在此处发布,以便我接受。 因此,这对我有用:
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;
        }
这是我的布局
main.xml
文件:
<?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>
Main.java中的其余代码(我修改了
getView
方法)从此处几乎逐字记录 再次感谢您的帮助!     

要回复问题请先登录注册