java – gridview/adapter issue, adding images to gridview-ThrowExceptions

Exception or error:

first do I have to make my own adapter class to set images in a gridview? its seams ineligent, requires I make a new class each time I make a gridview.

the problem I have having is that I can’t make my image appear in my gridview

package joshpike.hsh.hsh_game;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;


public class DisplayActivity extends MainActivity
{
    public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView imageView;
            if (convertView == null) 
            {  
                // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } 
            else 
            {
                imageView = (ImageView) convertView;
            }

           imageView.setImageResource(imageArray[position]);
            return imageView;
        }


        private Integer[] imageArray = 
        {
            //currentImageView(0, "xh","bottom",0,0), currentImageView(0, "xh","bottom",1,0)    
            R.drawable.level_1_bottom, R.drawable.level_1_top
        };


        @Override
        public int getCount() 
        {
            return 0;
        }

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

        @Override
        public long getItemId(int position) 
        {
            return 0;
        }
    }


    // Inflate the menu; this adds items to the action bar if it is present.
    //makes the options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.display, menu);
        return true;
    }

    //what happens if you select items from the options menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId() )
        {
            case R.id.miniMap:

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //called when activity is started for first time either for first time or after destroyed
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);

        GridView bottomMapGrid = (GridView)findViewById(R.id.bottomMapGrid);

        bottomMapGrid.setAdapter(new ImageAdapter());

        System.out.println("DisplayActivity onCreate done");        
    }
How to solve:
 @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        GridView bottomMapGrid = (GridView)findViewById(R.id.bottomMapGrid);
        bottomMapGrid.setAdapter(new ImageAdapter(this));
        System.out.println("DisplayActivity onCreate done");        
    }

And Adapter class Use like this

public class ImageAdapter extends BaseAdapter {
  private Context mContext;

  public ImageAdapter(Context c) {
      mContext = c;
  }

  public int getCount() {
      return mThumbIds.length;
  }

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

  public long getItemId(int position) {
      return 0;
  }

  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
      if (convertView == null) { // if it's not recycled, initialize some
                                  // attributes
          imageView = new ImageView(mContext);
          imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          imageView.setPadding(8, 8, 8, 8);

      } else {
          imageView = (ImageView) convertView;
      }

      imageView.setImageResource(mThumbIds[position]);

      return imageView;
  }

  // references to our images
  private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3,
          R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
          R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1,
          R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
          R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
          R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
          R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
          R.drawable.sample_6, R.drawable.sample_7 };
}

###

currently you are passing Null mContext to ImageView Constructor for Creating it. you need to initialize mContext before using it as :

public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

        public ImageAdapter(Context mContext){
           this.mContext=mContext;  //<<< initialize here
        }
         //....your code here
    }

and from Activity Create ImageAdapter instance by passing Current Activity Context as :

bottomMapGrid.setAdapter(new ImageAdapter(DisplayActivity.this));

Leave a Reply

Your email address will not be published. Required fields are marked *