1

My goal is to get data from DB, and then display it as a table in my app.

Here is my code:

 public void updateLastExpenses(){
    Cursor c = Expense.getAll(this);

    TableLayout lastExpensesTable = (TableLayout)findViewById(R.id.lastExpensesTable);

    lastExpensesTable.setStretchAllColumns(true);

    while (c.moveToNext()) {

        String name = c.getString(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_NAME)
        );
        float amount = c.getFloat(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_AMOUNT)
        );

        String date =  c.getString(
                c.getColumnIndexOrThrow(ExpenseContract.ExpenseEntry.COLUMN_NAME_DATE)
        );

        TableRow tr = new TableRow(this);

        TextView c1 = new TextView(this);
        c1.setText(name);
        c1.setBackgroundColor(Color.RED);

        TextView c2 = new TextView(this);
        c2.setText(""+amount);
        c2.setBackgroundColor(Color.BLUE);


        tr.addView(c1);
        tr.addView(c2);

        lastExpensesTable.addView(tr);

    }

}

And here is my TableLayout:

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lastExpensesTable">
            <TableRow
                android:id="@+id/lastExpensesTableRow"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/lastExpensesTableName"
                    android:text="@string/last_expenses_table_name"/>
                <TextView />
                <TextView
                    android:id="@+id/lastExpensesTableAmount"
                    android:text="@string/last_expenses_table_amount"/>
                <TextView />
            </TableRow>
        </TableLayout>

But here is the result:

enter image description here

Do you know why the content is all in the "name" column and not split between the two columns? Is there a cleaner way to do it (by using layout and avoiding the styling in the .java file I guess?)

Thanks in advance.

Vico
  • 1,696
  • 1
  • 24
  • 57

1 Answers1

1

You have a stray TextView in your layout.

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lastExpensesTable">
        <TableRow
            android:id="@+id/lastExpensesTableRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!-- first column -->
            <TextView
                android:id="@+id/lastExpensesTableName"
                android:text="@string/last_expenses_table_name"/>
            <!-- second column -->
            <TextView />
            <!-- third column -->
            <TextView
                android:id="@+id/lastExpensesTableAmount"
                android:text="@string/last_expenses_table_amount"/>
        </TableRow>
    </TableLayout>

Take out that TextView marked "second column" and you're all set.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • Thanks for your reply. Nevertheless, your solution is not the good one. You can see the table headers (name & amount) taking the whole width of the device. The problem is that the value (dynamic) are all put in the "name" column. – Vico Mar 30 '15 at 01:14
  • Well, you had an extra empty TextView in your layout between the two labeled TextViews, I took that out in my answer. Did you add the headers using findViewById()? If you did, you put the "Amount" header in the third column, not the second. But since you used addView for the data, the amounts came out in the second column like you expected. Sorry, I should have pointed that out, didn't occur to me that that was the problem you were trying to solve. – kris larson Mar 30 '15 at 01:22
  • No the header are added directly in the layout file, it's "@+id/lastExpensesTableAmount" and ""@+id/lastExpensesTableName". Then I get the table thanks to "@+id/lastExpensesTable" and try to add new rows programmatically. All my code is provided in my post. Thanks again. – Vico Mar 30 '15 at 01:26
  • 1
    Okay, that makes sense. See the one line in your layout that just says ""? That's the second column. So "Amount" is your third column. Take out that empty TextView and it should work. – kris larson Mar 30 '15 at 01:29