我想在网格布局中显示不同的图像 的 MainActivity.java
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isReadStorageAllowed()){
Intent intent = new Intent(MainActivity.this, GridActivity.class);
intent.putExtra("firstKeyName","FirstKeyValue");
intent.putExtra("secondKeyName","SecondKeyValue");
MainActivity.this.startActivity(intent);
}
});
我想在GridLayout中显示图片 我的 GridActivity.java 代码如下
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
// Get the view from new_activity.xml
setContentView(R.layout.activity_grid);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id){
}
});
在ImageAdapter中,我希望获得MainActivity的意图值,以便在图像中使用它(使用不同按钮在单个视图上使用不同的图像)
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
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) {*/
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setPadding(8, 8, 8, 8);
/*}
else
{
imageView = (ImageView) convertView;
}*/
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.b1, R.drawable.b2, R.drawable.b3,
R.drawable.b4, R.drawable.b5, R.drawable.b6,
R.drawable.b7, R.drawable.b8, R.drawable.b9,
R.drawable.b10, R.drawable.b11, R.drawable.b12,
};
答案 0 :(得分:0)
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int i) {
return mThumbIds[i];
}
@Override
public long getItemId(int i) {
return mThumbIds[i];
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
/*if (convertView == null) {*/
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setPadding(8, 8, 8, 8);
/*}
else
{
imageView = (ImageView) convertView;
}*/
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.b1, R.drawable.b2, R.drawable.b3,
R.drawable.b4, R.drawable.b5, R.drawable.b6,
R.drawable.b7, R.drawable.b8, R.drawable.b9,
R.drawable.b10, R.drawable.b11, R.drawable.b12,
};
}