Android : GridView adapter getItemID settings

on Sunday, February 1, 2015


I am filling a GridView with a set of images from MediaStore.Images.Thumbnails._ID and MediaStore.Images.Thumbnails.IMAGE_ID but get a diffent picture showing in the setOnItem ClickListener when shown in the Imageview in SecondActivity. (I am relatively new to cursors and databases so may be missing a few basics here!):



final Intent intent = new Intent(this, SecondActivity.class);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
intent.putExtra("picture",position);
startActivity(intent);


I am not getting the correct "position" and understand that I must modify the class below



//---Returns the ID of one item---
public long getItemId(int position) {
return position;
}


but I do not know how to get a reference to it or what to put there. The second intent always shows a different image from the clicked one.



int imageID = getIntent().getIntExtra("picture",0);
imageView.setImageURI(
Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, + imageID)
);

0 comments:

Post a Comment