0

首先我必须制作自己的适配器类来在gridview中设置图像吗?它的接缝不合理,每次制作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");        
    }
4

2 回答 2

0
 @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");        
    }

和适配器类这样使用

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 };
}
于 2013-03-13T05:03:27.527 回答
0

目前您正在将 Null 传递mContextImageView构造函数以创建它。您需要mContext在使用它之前进行初始化:

public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

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

并通过将当前活动上下文传递为 Activity 创建ImageAdapter实例:

bottomMapGrid.setAdapter(new ImageAdapter(DisplayActivity.this));
于 2013-03-13T03:35:38.527 回答