提问者:小点点

缩略图与gridview中的实际图像不同


我在 android 中创建一个简单的 GridView 应用程序,其中单击时的图像会在新窗口中以全屏形式打开。

应用程序图像:

我还将随机图像生成代码放在getView方法中,如下所示:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView iv;
    shuffleArray(images);
    if(arg1!=null){
        iv=(ImageView)arg1;
    }else{
        iv=new ImageView(context);
        iv.setLayoutParams(new GridView.LayoutParams(80,80)); //resizing the picture according to the params
        iv.setScaleType(ScaleType.CENTER_CROP);//images will be cropped towards center
        iv.setPadding(8, 8, 8, 8); //padding of all sides if images vary then less padding more crop to fit imageview
    }

    iv.setImageResource( images[arg0]);
    return iv;
}

static void shuffleArray(int[] images2) {
    // TODO Auto-generated method stub
    Random rnd=new Random();
    for(int i=images2.length-1;i>=0;i--){
        int index=rnd.nextInt(i+1);
        int temp=images2[index];
        images2[index]=images2[i];
        images2[i]=temp;
    }

}

每次我打开应用程序时,图片都会出现一些问题。

现在的问题如下:

>

  • 每当我单击图像缩略图时,都会在新窗口中打开不同的图像。

    GridView Activity中显示了两个或多个不同图像的相同图像缩略图。

      < li >每当用户点击特定数量的图像时,应用程序应自动关闭并显示消息。

    我应该如何解决上述问题?

    我的整个源代码:主要活动.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gv=(GridView)findViewById(R.id.gridView);
        gv.setAdapter(new ImageAdapter(getApplicationContext()));
    
        gv.setOnItemClickListener(new OnItemClickListener(){
    
            private boolean[] isSpecificImage;{
                isSpecificImage[0]=true;
            }
    
    
    
    
    
            @Override
            public void onItemClick(AdapterView<?> parentView, View iv, int position, long id) {
                // TODO Auto-generated method stub
    
                    Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
                    Intent i=new Intent(getApplicationContext(),ImageDetail.class);  // for click open that image as new activity
                    i.putExtra("id", position);
                    startActivity(i);
    
    
    
            }
    
        });
    

    ImageAdapter类

    public class ImageAdapter extends BaseAdapter {
    

    私人语境;public static int[]images={R.drawable.goku,R.drawaable.gohan,R.dravable.vegeta,R.dracable.browly,R.drawbable.buu};

    public ImageAdapter(Context applicationContext) {
         context = applicationContext;
            shuffleArray(images);
    }
    
    @Override
    public int getCount() {
        // no of items to be displayed
        return images.length;
    }
    
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv;
    
        if(arg1!=null){
            iv=(ImageView)arg1;
        }else{
            iv=new ImageView(context);
            iv.setLayoutParams(new GridView.LayoutParams(80,80)); //resizing the picture according to the params
            iv.setScaleType(ScaleType.CENTER_CROP);//images will be cropped towards center
            iv.setPadding(8, 8, 8, 8); //padding of all sides if images vary then less padding more crop to fit imageview
        }
    
        iv.setImageResource( images[arg0]);
        return iv;
    }
    
     void shuffleArray(int[] images2) {
        // TODO Auto-generated method stub
        Random rnd=new Random();
        for(int i=images2.length-1;i>=0;i--){
            int index=rnd.nextInt(i+1);
            int temp=images2[index];
            images2[index]=images2[i];
            images2[i]=temp;
        }
    
    }
    

    }

    图像细节类

    public class ImageDetail extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagedetail);
        Intent i=getIntent();
        int position =(Integer) i.getExtras().get("id");
        ImageView iv=(ImageView)findViewById(R.id.ivdbz);
        iv.setImageResource(ImageAdapter.images[position]);
    }
    

    }


  • 共2个答案

    匿名用户

    您可以使用OnClickItemListener中的位置来放置< code>finish()

    gridView中的条件

    匿名用户

    shuffleArray(images); 
    

    该语句不属于getView()。不应在每次调用getView()时都进行洗牌。

    换个地方打吧。