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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:01:37+00:00 2026-05-20T16:01:37+00:00

Having a hard time figuring out the best way to go about this. What

  • 0

Having a hard time figuring out the best way to go about this. What I’ve got going on is a gallery view loaded with images, with a text view just beneath it. I would like to fill the contents of the textview based upon which image is clicked. I followed the standard GalleryView tutorial where it has you create a custom ImageAdapter class which extends the BaseAdapter class. In doing so, I created an OnItemClickListener for the galleryview, and I assumed the next logical step would be to create a switch statement to figure out what to put in the text view.

So I eventually figured out that I should probably be itererating over the gallery items as opposed to the parameters passed to the onitemclicklistener() method. The problem is now, no matter what item is clicked, the output to the desired text view is always as if the last item was clicked. I commented out the ‘case 2’, and it then began to always take case 1. What would be causing the switch statement to ‘default’ to the last case coded even though I have not defined a default case?

Additionally, I’m seeing conflicting stuff regarding the switch statement in terms of looping. It should loop on its own, yes? If so I imagine it probably is, but since it always selects case 2, I’m not realizing that the output of the text should be changing? Is my switch statement all messed up? do I need to include some sort of loop? What am I doing wrong here?

Custom ImageAdapter class:

 public class ImageAdapter extends BaseAdapter
{
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c) 
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
         R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();                    
        }
 //---returns the number of images---
 public int getCount() {
     return imageIDs.length;
 }
 //---returns the ID of an item--- 
 public Object getItem(int position) {
     return position;
 }            

 public long getItemId(int position) {
     return position;
 }

 //---returns an ImageView view---
 public View getView(int position, View convertView, ViewGroup parent) {
     ImageView imageView = new ImageView(context);
     imageView.setImageResource(imageIDs[position]);
     imageView.setScaleType(ImageView.ScaleType.FIT_XY);
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
     imageView.setBackgroundResource(itemBackground);
     return imageView;
 }
}
}

And here is the code I’m using to create the galleryview and the onitemclicklistener:

Gallery gallery = (Gallery) findViewById(R.id.top_gallery);
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView parent, 
                View v, int position, long id) 
                {
                    TextView tView = (TextView) findViewById(R.id.cat_desc);
                    switch(gallery.getSelectedItemPosition())
                    {   
                    case 0:
                        tView.setText("Option1");

                    case 1:
                        tView.setText("Option2");

                    case 2:
                        tView.setText("Option3");
}
    }               
  • 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-20T16:01:38+00:00Added an answer on May 20, 2026 at 4:01 pm

    You’re lacking break’s in case statement and code gets executed in linear way. That’s why the last is always selected.

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

Sidebar

Related Questions

Having a hard time figuring out the best way to do this... I have
I'm having a hard time figuring out the best way to pass simple values
Not the biggest whiz on math here... having a hard time figuring this out.
I am having a hard time figuring out this problem. My app has iAds
I'm having a hard time figuring out how to architect the final piece of
I'm having a hard time figuring out how Firefox and Chrome determining what fields
My company is having trouble figuring out the best way to manage our builds,
I'm having a hard time figuring out how to implement a factory pattern in
I'm having a hard time figuring out what I'm doing wrong. I want do
I'm having a really hard time figuring out the scoping issue with the following

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.