Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridview return only one row

I am using a GridView in a Relative layout. I want the GridView to show up as tall as needed to show all the rows. However, I am seeing only one row when set (layout_width=MATCH_PARENT and) layout_height=WRAP_CONTENT. If I set layout_height to a size that equals mulitple rows, then I do see those rows.

<?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:gravity="center"
>

<RelativeLayout
    android:id="@+id/layout_month"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <TextView
        android:id="@+id/textv_month_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:gravity="center_horizontal|center_horizontal"
        android:textColor="#ffffff"
        android:textSize="7dp" />

    <ImageView
        android:id="@+id/top_bar"
        android:layout_width="fill_parent"
        android:layout_height="15dp"
        android:layout_below="@+id/textv_month_name"
        android:background="@drawable/calendar_top_small"
        android:clickable="false" />

    <GridView
        android:id="@+id/gridv_inner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:layout_gravity="center"
        android:clickable="false"
        android:gravity="center"
        android:numColumns="7"
        android:stretchMode="columnWidth"
         >
    </GridView>

    <Button
        android:id="@+id/monthBtn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" />
</RelativeLayout>

</RelativeLayout>

Any Help will be appreciable

like image 357
Arun Antoney Avatar asked Mar 28 '14 10:03

Arun Antoney


People also ask

How do I read all the rows in a GridView?

With a for-loop read all the rows in your gridview, if the status field of the specific row is closed, get its row index and set its readonly property to "true".

What is the use of for-loop in a GridView?

Please Sign up or sign in to vote. With a for-loop read all the rows in your gridview, if the status field of the specific row is closed, get its row index and set its readonly property to "true".

How to update a row with changed data in a grid?

To update a Grid row with changed data, you need to call the RefreshRow method. Attached is a small video and sample, which illustrate what I mean. As for your scenario, your approach is correct.

What happens if the GridView is not bounded?

If the GridView is not bounded than RefreshRow also cannot do anything because where to gets its data from ? I have a method now that fixes my issue so there is no problem right now but I would like to understand what RefreshRow is for. When Grid is bound to a data table, this data is stored in memory.


1 Answers

This is a very useful answer.

The GridView didn’t like the wrap_content of its height when it was put in a vertically tight space; it only showed a single row of items (scrolling all content in that single row).

The short version of my fix includes sub classing the GridView and overriding the onMeasure method. The essence of the fix focuses on setting the measured height spec to a ridiculously large number.

MyGridview.Java

   package com.arun.calendar;

   import android.content.Context;
   import android.util.AttributeSet;
   import android.widget.GridView;

   public class MyGridView extends GridView {

 public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSpec;

        if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {

            // The two leftmost bits in the height measure spec have
            // a special meaning, hence we can't use them to describe height.
            heightSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
        }
        else {
            // Any other height should be respected as is.
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}

And the new XML file

   <?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:gravity="center" >

<RelativeLayout
    android:id="@+id/layout_month"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textv_month_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:gravity="center_horizontal|center_horizontal"
        android:textColor="#ffffff"
        android:textSize="7dp" />

    <ImageView
        android:id="@+id/top_bar"
        android:layout_width="fill_parent"
        android:layout_height="15dp"
        android:layout_below="@+id/textv_month_name"
        android:background="@drawable/calendar_top_small"
        android:clickable="false" />

    <com.arun.calendar.MyGridView
        android:id="@+id/gridv_inner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:layout_gravity="center"
        android:clickable="false"
        android:gravity="center"
        android:numColumns="7"
        android:stretchMode="columnWidth" />
</RelativeLayout>

<Button
    android:id="@+id/monthBtn"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent" />

</RelativeLayout>

And in java file

 MyGridView  viewHolder.gridView = (MyGridView) v.findViewById(R.id.gridv_inner);
like image 156
Arun Antoney Avatar answered Oct 12 '22 10:10

Arun Antoney