0

我在 aImageView旁边有一个GridView,我想要做的是将 设置ImageView为在GridView.

当我运行我的项目时,所有缩略图都正常显示在GridViewphotoGrid_)中,但是当我按下缩略图时,Toast显示正确的索引,但我的ImageViewcurrentImage_)不显示任何内容。我尝试过类似的解决方案,但我要么得到相同的结果,要么我的应用程序将崩溃。

我的活动

public void gridViewArea()
{
     photoGrid_ = (GridView) findViewById(R.id.gridview);
     photoGrid_.setAdapter(imageAdapter_);

     photoGrid_.setOnItemClickListener(new AdapterView.OnItemClickListener()
     {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id)
         {
             Toast.makeText(MyActivity.this, "" + position, Toast.LENGTH_SHORT).show();

             currentImage_ = (ImageView) imageAdapter_.getView(position, view, parent);
             currentImage_.setImageResource(imageAdapter_.getSelectedImage()[position]);

         }
     });
}

适配器

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;
}

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(0, 0, 0, 0);
    }
    else
    {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);

    return imageView;
}

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
};

public Integer[] getSelectedImage()
{
    return  mThumbIds;
}

}

4

1 回答 1

0

通过做currentImage_ = (ImageView) imageAdapter_.getView(position, view, parent);

您正在覆盖 currentImage_ 是什么。

我想它应该指向您在布局中拥有的图像视图。您的应用程序不会因为 gridview 的元素也是 imageviews 而崩溃。

currentImage_ 应该始终指向细节 ImageView 并且您应该只通过执行来更改其内容

             currentImage_.setImageResource(imageAdapter_.getSelectedImage()[position]);

简而言之,删除上一行。

于 2013-09-05T11:16:39.383 回答