我正在尝试使用LayoutInflator填充gridview。.据我说,代码是正确的,但是我只得到一个空白屏幕作为输出...如果有人可以告诉我我在做什么错以及如何纠正它。预先

活动课

 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>


提前谢谢

最佳答案

您应该更改适配器类:

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;

      }

  }

10-08 18:34