0

I have gridview and have to download images to gridview. When I open app I cannot see any images, but I think my code is somehow correct. How to fix it? I use this code in my adapter:

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        //float wd = 250 / Resources.getSystem().getDisplayMetrics().density;
        //float hg = 300 / Resources.getSystem().getDisplayMetrics().density;
        int pxwd = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 90, Resources.getSystem().getDisplayMetrics());
        int pxhg = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 120, Resources.getSystem().getDisplayMetrics());
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(pxwd, pxhg));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageDrawable(LoadImageFromURL(GridViewConfig.getImage_list().get(position)));
    return imageView;
}


// references to our images
private Drawable LoadImageFromURL(String url)
{
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        return Drawable.createFromStream(is, "src");
    } catch (Exception e) {
        return null;
    }
}

and here is url list:

public class GridViewConfig {
public static ArrayList<String> image_listGrid = new ArrayList<String>();

public static ArrayList<String> getImage_list() {
    return image_listGrid;
}

public static void setImage_list(ArrayList<String> image_list) {
    GridViewConfig.image_listGrid = image_list;
}

public static void addImageUrls(){
    image_listGrid.add("someimage.jpg");
}

But problem is no image is showing on my GridView.

Chingiz
  • 332
  • 5
  • 17

1 Answers1

0

In your getView(...), you should use TypedValue.COMPLEX_UNIT_DIP replaces TypedValue.COMPLEX_UNIT_SP:

int pxwd = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, Resources.getSystem().getDisplayMetrics());
int pxhg = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, Resources.getSystem().getDisplayMetrics());

Hope this help!

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33