set the Columns of a TableRow in a TableLayout

1.3k Views Asked by At

well basicly I'm getting data from a database and I want to adapt this data in a kind of "DataGridView" on any other lenguaje, Basicly I have an GridView in a Main layout define like this:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gastoGridView"
    android:stretchMode="columnWidth"
    android:cacheColorHint="#00000000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="1"
    android:clipChildren="true"
    android:horizontalSpacing="5dip"
    android:verticalSpacing="5dip" />

other hand I have a second xml layout file than define every item(row) for this gridView, It is my item_gridview xml file:

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

    <TableRow android:id="@+id/TableRow01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">

            <TextView  android:text="@string/hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/fechaAtt"
                android:gravity="left"
                android:layout_gravity="left" />

            <TextView  android:text="@string/hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/descpAtt"
                android:gravity="center_horizontal"
                android:layout_gravity="center_horizontal" />

        <TextView  android:text="@string/hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/saldoAtt"
            android:layout_gravity="right" />

    </TableRow>
</TableLayout>

Ok, basicly I'm getting 3 attributes from database, I already developed the adapter to this gridview, so I show the information on it, but not in the way I want, I'll try to explain you, Currently it's being show like this

|     Date    |        Description        |      Price     |
 2014-11-17 any description it have 50.85$

I'm trying to divide my tablerow (width:fill_parent) on 3 sections(columns) I'm not sure if It's possible, because I'm not very involved on this subject, but I want to divide this tablerow on those 3 section, I want a small section on the left side of the row which will be my date, a large section in the center_horizontal wich will be my Description, and another left section wich will be my price, I'm not sure if you guys get my point but I want some like this.

|    Date       |          Description               |      Price      |
  2014-11-17      Get the description Centered Here        50.85$

I've tried to use the layout_span and layout_column on every TextView, but I get a Null Pointer error which I don't understand, maybe I'm doing that in a wrong way.

Could you guys help me to get this style? I've been reading about it a lot, It's a kind of difficult because Android do not support an DataGridView tool as others lenguages do.

Thanks you beforehand, I really need it

1

There are 1 best solutions below

5
On BEST ANSWER

Your difficulty stems from the fact that your trying to bring your concept of the DataGridView into Android which is problematic. What you really want to do use a ListView with a proper Adapter and Loader (use a Loader if possible).

Now, with a ListView what happens is it creates View for every row returned from the Cursor using the Adapter to create (inflate) this view and populate it (binding). This is useful since you can now think about each row as a set of three items and lay them out appropriately. I recommend just using the regular LinearLayout with the appropriate layout_weight set for your layout. You'll have to remember to set the LinearLayout to horizontal.

Edit:

For clarification. With LinearLayout you can specify in the layout.xml file the android:layout_weight parameter. This allows you to set 'relative' sizes (width or height depending on horizontal or vertical LinearLayout). Once you do this the android:layout_width is ignored but you should set it to 0dp. An Example:

<LinearLayout
    android:id="@+id/row_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
    <TextView
        android:id="@+id/date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    />
    <TextView
        android:id="@+id/description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    />
    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    />
</LinearLayout>

Now you have three TextView in a horizontal LinearLayout each with a weight set to 1. That would make them all equal size. You can adjust the weight values to change their relative sizes to each other and the parent width.