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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:18:55+00:00 2026-05-30T12:18:55+00:00

I am trying to populate a gridview using LayoutInflator.. According to me the code

  • 0

I am trying to populate a gridview using LayoutInflator.. According to me the code is correct but I am only getting a blank screen as output… if someone could tell me what am i doing wrong and how to rectify it.. Thanks in advance

Activity class

 public class MultipleDocumentViewActivity extends Activity {
   private LayoutInflater inflator = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           inflator = LayoutInflater.from(getApplicationContext());

           GridView grid = (GridView)inflator.inflate(R.layout.main, null);

           GridAdapter gAdapter = new GridAdapter(this);
           grid.setAdapter(gAdapter);
           setContentView(R.layout.main);


         }
        }

GridAdapter.java

     public class GridAdapter extends BaseAdapter
      {
     private Context context;
     private LayoutInflater layoutInflator = null;

     private Integer[] mThumbnails =                                                                                                           {R.drawable.kunfu1,R.drawable.kunfu2,R.drawable.kunfu3,R.drawable.kunfu4};

         public GridAdapter(Context c)
           {
          context = c;

          Log.d("Aditi","Constructor called");
           }
           public int getCount() {

         return mThumbnails.length;
           }

           public Object getItem(int position) {

          return null;
           }

           public long getItemId(int position) {

           return 0;
            }

           public View getView(int position, View grid, ViewGroup parent) 
           {
           layoutInflator =          (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         if(grid == null)
          {
         layoutInflator = LayoutInflater.from(context);



         grid = layoutInflator.inflate(R.layout.mdv_grid_item, null);

          }
          ImageView imageView =  (ImageView)grid.findViewById(R.id.grid_item);

          imageView.setImageResource(mThumbnails[position]);
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

          return grid;

          }

          }




     **mdv_grid_item**

<ImageView android:id="@+id/grid_item"
    android:layout_width="fill_parent" android:layout_height="270dp"
    android:contentDescription="@string/desc">
</ImageView>

**main.xml**
    <?xml version="1.0" encoding="utf-8"?>
      <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:horizontalSpacing="20dp" android:numColumns="3"
    android:paddingLeft="40dp" android:paddingRight="40dp"
    android:paddingTop="40dp" android:stretchMode="columnWidth"
    android:verticalSpacing="20dp"
    android:listSelector="#00000000"
    android:id = "@+id/grid">
       </GridView>

Thanks a lot in advance

  • 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-30T12:18:56+00:00Added an answer on May 30, 2026 at 12:18 pm

    you should change your adapter class:

    public class GridAdapter extends BaseAdapter
    {
        private Context context;
        private LayoutInflater layoutInflator;
    
        private Integer[] mThumbnails = {R.drawable.kunfu1,R.drawable.kunfu2,R.drawable.kunfu3,R.drawable.kunfu4};
    
        public GridAdapter(Context c){
          context = c;
          layoutInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         }
    
         public int getCount() {
              return mThumbnails.length;
         }
    
         public Object getItem(int position) {
              return null;
         }
    
         public long getItemId(int position) {
              return 0;
         }
    
         public View getView(int position, View grid, ViewGroup parent){
    
             ImageView imageView;
             if(grid == null) {
                 grid = layoutInflator.inflate(R.layout.mdv_grid_item, null);
                 imageView =  (ImageView)grid.findViewById(R.id.grid_item);
    
                 grid.setTag(imageView);
    
              }else{
                 imageView = (ImageView)grid.getTag();
              }
    
    
              imageView.setImageResource(mThumbnails[position]);
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    
             return grid;
    
          }
    
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to jQuery, trying to populate a GridView or Telerik RadGrid using
I am trying to populate a string with a double value using a sprintf
I'm trying to populate a DataTable, to build a LocalReport, using the following: MySqlCommand
I'm trying to populate a dictionary dynamically in a for loop like so: pseudo-code
I am trying to nest one gridview within another, but I cannot get the
I'm trying to populate a dropdown list inside a repeater, but I'm not being
I am trying to populate a grid using a DirectStore. No data appears in
I am trying to populate a select box (cities related to a state) using
I'm trying to populate a smartgwt calendar using data form a server obtained using
I'm trying to bind my GridView at runtime, but I'm also trying to avoid

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.