EzDevInfo.com

gridview interview questions

Top gridview frequently asked interview questions

How to disable GridView scrolling in Android?

I'm using a GridView for a game board. Recently some users have had problems with the board scrolling vertically (on Samsung Galaxy / Vibrant phones running 2.2) -- This bug does not occur on my Nexus One.

One user produced some screenshots of the problem.

How could I lock the GridView in place? Is there a way to disable scrolling?


Source: (StackOverflow)

making grid view items square

I would like the items of my GridView to be square. There are 2 columns and the items width is 'fill_parent' (e.g. they take as much horizontal space as possible. The items are custom views.

How do I make the items height to be equal to their variable width?


Source: (StackOverflow)

Advertisements

Android GridView with categories?

Is it possible to use categories or some sort of headers with a GridView in Android?

I put together a quick illustration of what I was thinking about:

enter image description here

Thanks a lot.


Source: (StackOverflow)

GridView inside Expandable list in android

I want to put an gridview with images inside an expandable list...I've already make that, but the gridview don't show all item...

IMAGE OF ACTUAL LAYOUT

How can i make my expandable list child adapt to the gridview size?

LIST ADAPTER

public class CustomListAdapter extends BaseExpandableListAdapter
{
    String[]            catg    = { "Administração, Escritorio e Industria", "Cultura e Entretenimento", "Educação e Crianças", "Eventos e Estado do tempo", "Amigos, Familia e Habitações", "Multimédia", "Diversos", "Números e Letras", "Restaurantes e Hoteis", "Desporto, Saude e Beleza", "Lojas", "Turismo e Natureza", "Transportes" };

    Context             myctx;


    @Override
    public Object getChild(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition)
    {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        ViewGroup item = getViewGroupChild(convertView, parent);
        GridView label = (GridView) item.findViewById(ipvc.estg.placebook.R.id.gridview);
        label.setAdapter(new GridAdapter(parent.getContext(), groupPosition+1));
        return item;
    }

    private ViewGroup getViewGroupChild(View convertView, ViewGroup parent)
    {
        // The parent will be our ListView from the ListActivity
        if (convertView instanceof ViewGroup)
        {
            return (ViewGroup) convertView;
        }
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewGroup item = (ViewGroup) inflater.inflate(ipvc.estg.placebook.R.layout.expandable_list_row, null);
        return item;
    }

    @Override
    public int getChildrenCount(int groupPosition)
    {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition)
    {
        return catg[groupPosition];
    }

    @Override
    public int getGroupCount()
    {
        return catg.length;
    }

    @Override
    public long getGroupId(int groupPosition)
    {
        return groupPosition;
    }



    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        View item = getViewGroupGroup(convertView, parent);

        TextView text = (TextView) item.findViewById(android.R.id.text1);
        text.setText(catg[groupPosition]);
        return item;
    }

    private View getViewGroupGroup(View convertView, ViewGroup parent)
    {
        // The parent will be our ListView from the ListActivity
        if (convertView instanceof View)
        {
            return (View) convertView;
        }
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View item1 = (View) inflater.inflate(android.R.layout.simple_expandable_list_item_1, null);

        return item1;
    }

    @Override
    public boolean hasStableIds()
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        // TODO Auto-generated method stub
        return true;
    }

}

LIST ROW LAYOUT

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="50dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center" />
</LinearLayout>

Source: (StackOverflow)

Difference between List View and DataGrid in WPF?

Hey I have to retrieve some questions from the database and display them on the user screen dynamically.I also need to add some controls in the columns of grid view,basically a question and input box for an answer.Please suggest which one should I use? List View or data Grid?


Source: (StackOverflow)

Create thumbnail image

I want to display the thumbnail image in grid view from file location . how to generate that of .jpeg file I am using C# language with asp.net


Source: (StackOverflow)

GridView VS GridLayout in Android Apps

I have to use a grid to implement Photo Browser in Android, and I want to know the difference between GridView and GridLayout.

I'm using GridView to display the images dynamically (from SD card).


Source: (StackOverflow)

How do I get Gridview to render THEAD?

How do I get the GridView control to render the <thead> <tbody> tags? I know .UseAccessibleHeaders makes it put <th> instead of <td>, but I cant get the <thead> to appear.


Source: (StackOverflow)

How to put GridView inside ScrollView

I have to design layout such that whole layout should scroll and inside layout I have to display related content in Grid form so I decided to use GridView.But problem is that I’m unable to use GridView inside ScrollView I have read documentation(Documentation also saying that we should not use GridView inside ScrollView) also.But I have to do this,so can any please give me some idea about this.Thanks


Source: (StackOverflow)

Gridview height gets cut

I'm trying to display 8 items inside a gridview. Sadly, the gridview height is always too little, so that it only shows the first row, and a little part of the second.

Setting android:layout_height="300dp" makes it work. wrap_content and fill_parent apparently not.

My grid view:

<GridView
    android:id="@+id/myId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

My items resource:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:minHeight="?android:attr/listPreferredItemHeight" >

    <ImageView
        android:id="@+id/appItemIcon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_info"
        android:scaleType="center" />      

    <TextView
        android:id="@+id/appItemText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My long application name"
        android:gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

The issue does not seem related to a lack of vertical space.

What can I do ?


Source: (StackOverflow)

How to autosize and right-align GridViewColumn data in WPF?

How can I:

  • right-align the text in the ID column
  • make each of the columns auto size according to the text length of the cell with the longest visible data?

Here is the code:

<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="40"/>
            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="100" />
            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
        </GridView>
    </ListView.View>
</ListView>

partial answer:

Thanks Kjetil, the GridViewColumn.CellTemplate works well and the Auto Width works of course but when the ObservativeCollection "Collection" is updated with longer-than-column-width data, the column sizes do not update themselves so that is only a solution for the initial display of data:

<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" Width="Auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Id}" TextAlignment="Right" Width="40"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" />
            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/>
        </GridView>
    </ListView.View>
</ListView>

Source: (StackOverflow)

GridView bound with Properties of nested class

I have an object map similar to what's listed below. When I try to bind the properties of NestedClass in a GridView I get the error:

"A field or property with the name 'NestedClass.Name' was not found on the selected data source."

The GridView is bound to an ObjectDataSource and the ObjectDataSource is bound to a fully populated instance of BoundClass.

Is there any way around this?

Sample classes:

public class BoundClass
{
    public string Name { get; set; }
    public NestedClass NestedClass { get; set; }
}

public class NestedClass
{
    public string Name { get; set; }
}

Source: (StackOverflow)

ASP.NET GridView second header row to span main header row

I have an ASP.NET GridView which has columns that look like this:

| Foo | Bar | Total1 | Total2 | Total3 |

Is it possible to create a header on two rows that looks like this?

|           |  Totals   |    
| Foo | Bar | 1 | 2 | 3 |

The data in each row will remain unchanged as this is just to pretty up the header and decrease the horizontal space that the grid takes up.

The entire GridView is sortable in case that matters. I don't intend for the added "Totals" spanning column to have any sort functionality.

Edit:

Based on one of the articles given below, I created a class which inherits from GridView and adds the second header row in.

namespace CustomControls
{
    public class TwoHeadedGridView : GridView
    {
        protected Table InnerTable
        {
            get
            {
                if (this.HasControls())
                {
                    return (Table)this.Controls[0];
                }

                return null;
            }
        }

        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);
            this.CreateSecondHeader();
        }

        private void CreateSecondHeader()
        {
            GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell left = new TableHeaderCell();
            left.ColumnSpan = 3;
            row.Cells.Add(left);

            TableCell totals = new TableHeaderCell();
            totals.ColumnSpan = this.Columns.Count - 3;
            totals.Text = "Totals";
            row.Cells.Add(totals);

            this.InnerTable.Rows.AddAt(0, row);
        }
    }
}

In case you are new to ASP.NET like I am, I should also point out that you need to:

1) Register your class by adding a line like this to your web form:

<%@ Register TagPrefix="foo" NameSpace="CustomControls" Assembly="__code"%>

2) Change asp:GridView in your previous markup to foo:TwoHeadedGridView. Don't forget the closing tag.

Another edit:

You can also do this without creating a custom class.

Simply add an event handler for the DataBound event of your grid like this:

protected void gvOrganisms_DataBound(object sender, EventArgs e)
{
    GridView grid = sender as GridView;

    if (grid != null)
    {
        GridViewRow row = new GridViewRow(0, -1,
            DataControlRowType.Header, DataControlRowState.Normal);

        TableCell left = new TableHeaderCell();
        left.ColumnSpan = 3;
        row.Cells.Add(left);

        TableCell totals = new TableHeaderCell();
        totals.ColumnSpan = grid.Columns.Count - 3;
        totals.Text = "Totals";
        row.Cells.Add(totals);

        Table t = grid.Controls[0] as Table;
        if (t != null)
        {
            t.Rows.AddAt(0, row);
        }
    }
}

The advantage of the custom control is that you can see the extra header row on the design view of your web form. The event handler method is a bit simpler, though.


Source: (StackOverflow)

How to set Grid row and column positions programmatically

I have two Grids inside a Stackpanel. First grid is named as GridX. Initially inside the grid there is a 2D array of Textboxes(RowDefs/ColumnDefs). The TextBox definition in XAML is

<TextBox x:Name="A1" Grid.Row="4" Grid.Column="5" TextAlignment="Center" />

I want to add a TextBlock programamtically in the same position as part of GridX.

Effect must be like this

<TextBlock Grid.Row="4" Grid.Column="5"
    HorizontalAlignment="Left" VerticalAlignment="Top" Text="10" FontSize="8"/>

How to add this. I have tried this:

TextBlock tblock = new TextBlock();
GridX.SetColumn(tblock, cIndex);
GridX.SetRow(tblock, rIndex);

But failed.

Again I tried this:

int rIndex = Grid.GetRow(txtBox);
int cIndex = Grid.GetColumn(txtBox);                               

                            TextBlock tblock = new TextBlock();
                            tblock.VerticalAlignment = VerticalAlignment.Top;
                            tblock.HorizontalAlignment = HorizontalAlignment.Left;
                            tblock.FontSize = 8;
                            tblock.Text = rc[i, j - 1];

                            Grid.SetColumn(tblock, cIndex);
                            Grid.SetRow(tblock, rIndex);


                            txtBox.MaxLength = 1;    

Now the problem is that TextBlock is not visible.TextBox hides it. Can you help


Source: (StackOverflow)

Why are there extra pixels around my Android GridView?

I have a GridView in my Android application that has a number of ImageViews in it. The space on my screen is limited and I want the images to take up as much of the available space as they can. Unfortunately, the GridView always leaves 5 pixels of empty screen space around the outside edge of the ImageViews (the space between ImageViews is set with horizontal/vertical spacing and behaves correctly). The empty space acts kind of like a margin around the ImageViews, but I can't get rid of it. Does anyone know what's causing this "border" and how I can get rid of it (or at least make it smaller)? Thanks.

Update: I'm creating the ImageViews by inflating an .xml file in the getView() method of my Adapter class. Here's the xml I'm inflating:

<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="#FF00FF" />

I defined the GridView in my layout xml file like this:

<GridView
   android:id="@+id/mygrid"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_above="@+id/abutton"
   android:layout_marginBottom="8dp"
   android:numColumns="5" android:background="#FF0000"/>

Here's a screen shot of the problem. The red area in my GridView. The purple areas are my ImageViews. The image being displayed is a simple blue rectangle with a transparent center. The image is 45x45 pixels (but is only 30x30 pixels in my app - I'll worry about that later). The red border around the purple is what I am trying to eliminate.

alt text


Source: (StackOverflow)