Search code examples
javaandroideclipsegridviewadt

GridView entries are being added from the bottom up


I want to add entries to my GridView starting at the top, but instead the first entry is registered at the bottom left. I have another piece of code in my project that adds entries to a GridView and the code is identical, and works how I want it to. I'm sure this is a simple fix, but I can't seem to figure out where the problem lies.

ScreenShot

Here is my layout code for the form in the picture

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ll1"
    android:layout_above="@+id/ll2"
     >

    <GridView
        android:id="@+id/gvObjects"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="3"
        android:horizontalSpacing="4dip"
        android:gravity="center"
        android:verticalSpacing="4dip"
        android:padding="4dip"
        android:stretchMode="columnWidth" >

    </GridView>
</LinearLayout>

<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignTop="@+id/btnDownloadObjects"
    android:orientation="vertical" >

    </LinearLayout>

<Button
    android:id="@+id/btnDownloadObjects"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="Download Objects"
    android:textSize="25sp" />

</RelativeLayout>

The next piece of code is my ImageView that is added within the GridView as an entry

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ivObject"
    android:layout_width="fill_parent"
    android:layout_height="100dip"
    android:adjustViewBounds="true"
    android:contentDescription="@string/descr_image"
    android:scaleType="centerInside" />

The following method registers the ImageView as an adapter for my GridView

private void populateGridView(){
        GridView gv = (GridView) findViewById(R.id.gvObjects);
        gv.setAdapter(new ImageAdapter());
        gv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

Finally, here is my ImageAdapter class

public class ImageAdapter extends BaseAdapter{
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ImageView imageView;
            if (convertView == null) {
                imageView = (ImageView) getLayoutInflater().inflate(R.layout.picture_list_object, parent, false);
            } else {
                imageView = (ImageView) convertView;
            }
            File picFile = new File(Global.hairFolder.get(position));
            Uri picUri = Uri.fromFile(picFile);
            System.out.println(picUri.toString());
            imageLoader.displayImage(picUri.toString(), imageView, options);
            return imageView;

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return Global.hairFolder.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    }

}

Solution

  • If I understood your problem correctly, it's the LinearLayout parent of your GridView:

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ll1"
        android:layout_above="@+id/ll2"
         >
    

    wrap_content + layout_above make the LinearLayout not take whole available vertical space and layout at the bottom.

    Looks like you could remove ll1 altogether and just put the GridView in the parent RelativeLayout. If you don't specify any special layout_... positioning attributes, it will be placed at the top left corner.