我有两个int数组图像,我已经在网格视图中实现了它。我想在点击网格上的项目时从另一个数组设置图像?我尝试通过全局创建imageview的对象,但它的力量接近。
package com.sachin.majum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class select extends Activity {
ImageView im = new ImageView(getApplicationContext());
int blank [] = {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launche r,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
int image []={R.drawable.dc,R.drawable.dd,R.drawable.kkr,R.drawable.mi,R.drawable.rcb,R.drawable.rr,R.drawable.dc,R.drawable.dd,R.drawable.kkr,R.drawable.mi,R.drawable.rcb,R.drawable.rr};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
Random random = new Random();
List intlist = new ArrayList();
for(int i=0;i<image.length;i++)
{
intlist.add(image[i]);
}
Collections.shuffle(intlist,random);
for(int i=0;i<intlist.size();i++)
{
image[i]=(Integer) intlist.get(i);
}
GridView grid = (GridView)findViewById(R.id.gv);
grid.setAdapter(new MyAdapter());
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
im.setImageResource(image[position]);
}
});
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return blank.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
im.setImageResource(blank[position]);
im.setLayoutParams(new GridView.LayoutParams(60,60));
return im;
}
}
}
`
答案 0 :(得分:2)
您需要为Gridview的每个单元格创建一个ImageView。我已经调整了你的getView以反映这一变化:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = new ImageView( select.this );
}
((ImageView)convertView).setImageResource(blank[position]);
((ImageView)convertView).setLayoutParams(new GridView.LayoutParams(60,60));
return convertView;
}
答案 1 :(得分:0)
更改您的onClickListener
代码。
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView iV = (ImageView)v;
iV.setImageResource(image[position]);
}
});
并删除此声明,您不需要它。
ImageView im = new ImageView(getApplicationContext());