aditi88 aditi88 - 6 months ago 13
Android Question

populating gridview using layout inflator

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

Answer

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;

      }

  }