首先我必须创建自己的适配器类来在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 destoryed
@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");
}
答案 0 :(得分:0)
目前您正在将Null mContext
传递给ImageView
构造函数以进行创建。您需要在使用之前初始化mContext
:
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context mContext){
this.mContext=mContext; //<<< initialize here
}
//....your code here
}
并通过将当前活动上下文传递为:
,从活动创建ImageAdapter
实例
bottomMapGrid.setAdapter(new ImageAdapter(DisplayActivity.this));
答案 1 :(得分: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");
}
和Adapter类像这样使用
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 }; }