1

I'm trying to add an image from your phones gallery into a gridView using its uri. Unfortunately it doesn't do anything and I can't seem to find a way to make it work.

My Adapter:

public class ImageGridAdapter extends BaseAdapter {


public Uri imageUri;


private Context mContext;

public ImageGridAdapter(Context c) {
    this.mContext = c;
}

@Override
public int getCount() {

    return 0;
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {

    imageUri = PictureGroupActivity.selectedImage;


    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageURI(null);
    imageView.setImageURI(imageUri);

    return imageView;
   }
}

The adpater should be taking the imageUri from onActivtyResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    gridView = (GridView)findViewById(R.id.picture_group_gridView);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Toast.makeText(PictureGroupActivity.this, "selectedImage ma hodnotu: " + selectedImage, Toast.LENGTH_SHORT).show();


    }
}

I've set the gridView in my onCreate inside my activity as so:

GridView gridView = (GridView)findViewById(R.id.picture_group_gridView);
    gridView.setAdapter(new ImageGridAdapter(this));

    gridView.invalidateViews();

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

When choosing a photo from the gallery, the gridView remains empty. How do you I make it work? What am I doing wrong ? Thank you in advance.

2 Answers 2

0
public class ImageGridAdapter extends BaseAdapter {


public Uri imageUri;


private Context mContext;

public ImageGridAdapter(Context c) {
    this.mContext = c;
}

@Override
public int getCount() {

    return 5;
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {

    imageUri = PictureGroupActivity.selectedImage;


    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageURI(null);
    imageView.setImageURI(imageUri);

    return imageView;
   }
}
2
  • Thank you, finally did something! :D It added the picture into the gridView (changed the 5 to 1) But when adding another picture it overwrites, how do you make it stay ?
    – Dunoo
    Commented Nov 16, 2015 at 16:19
  • change PictureGroupActivity.selectedImage to a ArrayList . Once you get a image, you store uri in it . and return size of list in getitemcount. Commented Nov 17, 2015 at 1:27
0

If you always return 0 from getCount(), then your list will always be empty. Similarly, you need to return something from getItem() so that your View knows what data is associated with which list item. You might want to use a more concrete Adapter implementation like ArrayAdapter that can handle some of this for you.

1
  • Fair enough, I returned 1 in getCount() , and now it displays it in the gridView, finally. I now want the image to stay there when I add another image, any help with that ?
    – Dunoo
    Commented Nov 16, 2015 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.