画廊和一个的LinearLayout之间切换 - ClassCastException异常 [英] Switch between Gallery and a LinearLayout - ClassCastException

查看:135
本文介绍了画廊和一个的LinearLayout之间切换 - ClassCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您阅读!

我建立一个自定义的图库应用程序,其中第一缩略图是专辑封面显示专辑的细节。这里的流量:

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
}

下面是实际的getView()code:

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;
            }
        }

cover.xml 包含一个的ImageView 和两个的TextView 秒。

当我在返回 convertView 其他块,我得到一个ClassCastException。当然,我做错了什么。

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 :(

请帮帮忙!

推荐答案

试图通过这里乐于助人的人给出的所有建议之后,我还是没能跨过ClassCastException异常就搞定了。

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;
        }

下面是我的布局的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>

在code在Main.java(其 getView 方法我修改),其余几乎是逐字从的这里

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

再次感谢您的帮助!

这篇关于画廊和一个的LinearLayout之间切换 - ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆