android数组索引异常

时间:2012-09-11 07:54:34

标签: android arrays

我有一个包含imageview和gridview的活动。规则是当我点击gridview上的所选图像时,它将被imageview预览。然后,可以点击imageview,它将在对话框中显示。问题是什么时候我点击了imageview,图片与gridview中的所选图片不匹配。我的代码。

public class GalleryActivity extends Activity {

ImageView imgviewThumb, imgviewPreviewDialog;
public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};
int position ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    imgviewThumb = (ImageView)findViewById(R.id.imgview_preview);
    imgviewThumb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog dialog = new Dialog(GalleryActivity.this);
            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialog_previewimage);
            imgviewPreviewDialog = (ImageView) dialog.findViewById(R.id.imgview_preview_dialog);
            imgviewPreviewDialog.setImageResource(imageResto[position]);
            dialog.show();
        }
    });

    GridView gView = (GridView) findViewById(R.id.grid_View);

    //Instantiating ImageAdapter class
    gView.setAdapter(new ImageAdapter(this));

    /*showing full image on clicking the thumbnail
     * create a new class named FullImage.java 
     */
    gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            imgviewThumb.setImageResource(imageResto[position]);
        }
    });
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }

    private Context mContext;

    // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };
}
}

请纠正我。谢谢你提前。

2 个答案:

答案 0 :(得分:1)

尝试更改:

public Object getItem(int position) {
        return position;
}

对此:

public Object getItem(int position) {
        return mThumbIds[position];
}

答案 1 :(得分:0)

我认为你使用两种不同的数组,因此它可能不匹配,

public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};

  // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };

并放置有效的图像阵列 这里的位置是正确的0到apter中的数组的长度所以它的写入位置到达这里

 gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            imgviewThumb.setImageResource(validImageArray[position]);
        }
    });

或者您可以在getView中执行此更改

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageResto[position]);

        return imageView;
    }