-1

我有两个 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;
}

}
}

`

4

2 回答 2

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;
}
于 2012-06-24T11:24:45.677 回答
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());
于 2012-06-24T11:51:43.210 回答