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

  • Home
  • SEARCH
  • 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 7004247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:10:28+00:00 2026-05-27T21:10:28+00:00

I am trying to reuse a framelayout that has an image and textview inside

  • 0

I am trying to reuse a framelayout that has an image and textview inside it but I don’t think I am doing this right. The code works and the display is right but the performance is very poor and I believe it is because I creating a new ImageView and TextView every time the adapters goes back to the item position.

Can someone tell me how to reuse the embedded ImageView (called i) and TextView (called t) without creating new objects? I am very new to Java and this is my attempt at building a Android application.

        public View getView(int position, View convertView, ViewGroup parent) {  

            FrameLayout F;
            FrameLayout ImageBorder;
            FrameLayout TextBG;

            ImageView i;
            TextView t;

            if(convertView == null) {
                F = new FrameLayout(mContext);

            } else {
                F = (FrameLayout) convertView;
            }

            ImageBorder = new FrameLayout(F.getContext());
            FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,300,Gravity.BOTTOM);
            ImageBorder.setLayoutParams(params1);

            i = new ImageView(F.getContext()); 
            TextBG = new FrameLayout(F.getContext());
            t = new TextView(F.getContext());

            F.setBackgroundColor(Color.BLACK);
            ImageBorder.setPadding(2, 2, 2, 2);
            ImageBorder.setBackgroundColor(Color.BLACK);

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,40,Gravity.BOTTOM);

            TextBG.setLayoutParams(params);
            TextBG.setBackgroundColor(Color.BLACK);
            TextBG.setAlpha(.6f);

            t.setLayoutParams(params);

            t.setGravity(Gravity.CENTER_VERTICAL);

            String pathToPhoto = FileList.get(position).toString();
            String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/","");

            fileDescription = fileDescription.replaceAll(".jpg","");
            fileDescription = fileDescription.toUpperCase();


            Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto);

             if (bm == null) {
                ImageDownloader downloader = new ImageDownloader(i);
                downloader.execute("thumb", pathToPhoto, "400", "400");
             } else {

                i.setImageBitmap(bm);
                i.setScaleType(ImageView.ScaleType.CENTER_CROP);

                t.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
                t.setText(" " + fileDescription);

             }

             ImageBorder.addView(i);
             ImageBorder.addView(TextBG);
             ImageBorder.addView(t);

             F.addView(ImageBorder);

             return F;  

        }  
    } 

Thank you in advance!

[EDIT]

————————— SOLUTION —————————————————–

Here is solution that I implemented based on the feedback below! Thank you!

        public View getView(int position, View convertView, ViewGroup parent) {  

            View ReturnThisView;
            ViewHolder holder;

            LayoutInflater inflater;
            holder = new ViewHolder();

            if(convertView == null) {
                inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ReturnThisView = inflater.inflate(R.layout.imagecell, null);
                ReturnThisView.setTag(holder);
            } else {
                ReturnThisView = convertView;
            }

            holder.TextDescription = (TextView) ReturnThisView.findViewById(R.id.PhotoDesc);
            holder.ImageThumbnail = (ImageView) ReturnThisView.findViewById(R.id.Thumbnail);

            String pathToPhoto = FileList.get(position).toString();
            String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/","");

            fileDescription = fileDescription.replaceAll(".jpg","");
            fileDescription = fileDescription.toUpperCase();

            Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto);

             if (bm == null) {
                ImageDownloader downloader = new ImageDownloader(holder.ImageThumbnail);
                downloader.execute("thumb", pathToPhoto, "400", "400");
             } else {

                holder.ImageThumbnail.setImageBitmap(bm);
                holder.ImageThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);

                holder.TextDescription.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
                holder.TextDescription.setText(" " + fileDescription);

             }

             return ReturnThisView;
        }  
    }

    static class ViewHolder {
    TextView TextDescription;
    ImageView ImageThumbnail;
}
  • 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-27T21:10:28+00:00Added an answer on May 27, 2026 at 9:10 pm
    1. Instead of dynamically creating your View for each element create an XML layout file, say row.xml.
    2. If you detect that convertView == null inflate new row by using inflater
    3. Locate your TextView and ImageView using View#findViewById
    4. Create a Holder object which will help hold references to your newly found TextView and ImageView
    5. Save holder as a tag so convertView.setTag(holder)
    6. For existing convertView find Holder object by doing holder = convertView.getTag()
    7. Set text and image for these two saved objects, e.g. holder.txt.setText("Foo")
    8. Android adapter will do the rest as far as reusing instances of inflated rows

    Arguably even for your code you can do view initialization and layout once and use Holder pattern to avoid re-initialization of the elements but I think XML will give you better experience

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

Sidebar

Related Questions

I am trying to reuse one code in ios application . That code has
I'm trying to reuse a small chunk of code inside a custom button class.
I am trying to reuse code from another project, but while copying the classes
I am trying to reuse an existing code ... but with no success .
Im trying to reuse a code that i've made for toggleswitch on my application
I'm trying to reuse some script that I have working on another page, but
I'm trying to reuse variables in my Code Igniter Model but I can't quite
I am trying to reuse a piece of jQuery code accordion code that I
I'm trying to reuse the same code I've always used but now it is
I am trying to reuse shape data to dynamically create new shapes in code.

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.