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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:33:00+00:00 2026-05-23T13:33:00+00:00

I have a grid view which is populated using a custom ImageAdapter class extending

  • 0

I have a grid view which is populated using a custom ImageAdapter class extending BaseAdapter.
The images are dynamically loaded from a particular folder in the SD card. I have named the images according to their postition (1.png, 2.png etc.). I have also set an OnClickListener for the grid items: an audio file with the same name as the image is played from the SD card.

It works well when the number of images is less and fits on a screen.

But when the number is large and the images doesn’t fit on a screen, the next set of rows displayed by scrolling the screen downwards is mostly repetition of images from the first few rows rather than the images at the corresponding position.

I find from the logcat that the getView() function of the adapter class gets called initially only for the images which are visible on the screen and while scrolling downwards, its not being called properly for further positions

Also sometimes the entire set of images gets re-arranged.
Should I do anything different from the basic implementation of grid view for properly displaying large number of images? Is there anything else I must be taking care of?

EDIT – CODE

I’m setting each tab using

tabGrid[i].setAdapter(new ImageAdapter(this,i));

This is the image adapter class

@Override
public int getCount() {
    // fileNames is a string array containing the image file names : 1.png, 2.png etc
    return fileNames.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    // I did not use this function
    return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View v;
     if(convertView==null) {
         LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = inflater.inflate(R.layout.grid_image, null);
         ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
         String bitmapFileName = fileNames[position];
         Bitmap bmp =(Bitmap)BitmapFactory.decodeFile(dir.getPath() + "/" + bitmapFileName);a
         iv.setImageBitmap(bmp);
     }
     else {
     v = convertView;
     }
     return v;
}

Does the getItem() and getItemId() functions matter? The directories and file names are all valid.

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

    Here’s a quick fix which should be better.

    @Override
    public String getItem(int position) {
        return fileNames[position];
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         View v;
         if(convertView==null) {
             LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             v = inflater.inflate(R.layout.grid_image, parent, false);
         }
         else {
            v = convertView;
         }
    
         ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
         String bitmapFileName = getItem(position);
         Bitmap bmp =(Bitmap)BitmapFactory.decodeFile(dir.getPath() + "/" + bitmapFileName);a
         iv.setImageBitmap(bmp);
    
         return v;
    }
    
    1. I filled getItem, it’s not 100% needed but it’s always better to have it. The rest of your adapter code can then rely on it
    2. The item id should be different for every entry, you could either use getItem(position).hashCode() (might be slower) or just return position (which I did here).
    3. The getView method is a bit more tricky. The idea is that if the convertView is null, you create it. And then, in every case, you set the view’s content.
    4. The inflate in the getView item should use the parent as parent, and the “false” is there to tell the system not to add the new view to the parent (the gridview will take care of that). If you don’t, some layout parameters might get ignored.

    The erorr you had was because the views were getting recycled (convertView not null) and you weren’t setting the content for those. Hope that helps !

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

Sidebar

Related Questions

I have a usercontrol to provide a data grid view and using it in
I have an image in a view which contains a 9x9 grid. I want
I have a page which has a GridView on it, which is populated from
I have a grid view which I am binding with data table. My problem
I am currently developing an ASP.net c# application. I have a grid view which
I have a Data Grid View inside a control that is displayed in a
I have a page with .Net grid view with about 12 text fields per
How to implement CEditListCtrl?. List control with edit capabality (Report/Grid view). I have a
I have a Gridview in which no rows populated initially. means i am not
I have a WPF ListView control for which I am dynamically creating columns. One

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.