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 4440526
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:04:03+00:00 2026-05-21T13:04:03+00:00

Thanks for reading! I am building a custom Gallery app where the first thumbnail

  • 0

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 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-21T13:04:04+00:00Added an answer on May 21, 2026 at 1:04 pm

    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!

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

Sidebar

Related Questions

I'm building a web app and want to enable logging/error tracking. After reading the
and thanks for reading. I am building a data entry form. I am trying
Thanks for reading. I'm a bit new to jQuery, and am trying to make
Thanks for reading this. I am dynamically generating some data which includes a select
Thanks for reading this. I have markup similar to what is below. Using the
Thanks for reading this I thought I could use find(), but couldn't make it
Thanks for reading this. I would have thought it would be as simple as
I'm reading about Rails fixtures in this guide (thanks, trevorturk). It appears you define
I'm building my first website for an internship. My instructors always told me to
I am reading The thread building block book. I do not understand this piece

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.