Reinherd
Reinherd

Reputation: 5504

GridView isn't being displayed correctly

I took a screenshot of it. Its displaying some weird way: enter image description here

This is the code.

The GridAdapter:

public class GridViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<Uri> mUrls;  
// references to our images

public GridViewAdapter(Context c, ArrayList<Uri> images) {
    mContext = c;
    this.mUrls = images;
}

public int getCount() {
    return mUrls.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ImageView inflatedImageView = (ImageView) mInflater.inflate(R.layout.imageview_amb_background, null);

    //ImageView imageView = new ImageView(mContext);

    inflatedImageView.setImageURI(mUrls.get(position));

    inflatedImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

    return inflatedImageView;
}

And the inflatedImageView is a layout inflate, like this:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:background="@drawable/bgimages" 
    android:maxWidth="120dp"
    android:padding="5dp">

</ImageView>

On the other hand, I've a gridView in a xml file:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="250dp"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:verticalSpacing="5dp" >
</GridView>

So, I inflate this gridView, I add several URI's (3 exactly) in a loop. Once added, I set the adapter to the gridview:

ArrayList<Uri> uriFotos = new ArrayList<Uri>();
HashMap<Integer, ListItem> items = xmlList.getItems();
for (int i = 0; i<items.size();i++){
     ListItem itemActual = items.get(i);
     itemActual.getLogoSrc();
     uriFotos.add(Uri.parse(Environment.getExternalStorageDirectory().toString()+rutaFinsCarpetaClient+itemActual.getLogoSrc()));
 }
 gridViewInflated.setAdapter(new GridViewAdapter(this,uriFotos));
variableContent.addView(gridViewInflated);

The images are "linked" correctly.

variableContent is a LinearLayout inside a ScrollView, so the grid should be scrollable. But as you can see few things are happening:

Hope you guys can help me. I've changed lots of layouts, changed widths, heights, and the same thing is happening.

Note that the image you see that is in the gridView, is something like 1200x800px.

Edit The same code with smaller images:

enter image description here

Upvotes: 1

Views: 606

Answers (1)

Guian
Guian

Reputation: 4708

I would try to set the Image view size to wrap_content :

android:layout_height="wrap_content"

and if you really need the 120dp height, try to reset this size after having set the image src:

 inflatedImageView.setImageURI(mUrls.get(position));

 inflatedImageView.setLayoutParams(new GridView.LayoutParams(120, 120));

also set the scaleType before adding the picture (preferably in the xml) :

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:scaleType="centerInside"
    android:background="@drawable/bgimages" 
    android:maxWidth="120dp"
    android:padding="5dp">

</ImageView>

Upvotes: 2

Related Questions