1

I'm trying to display a set of data in a Grid style, using a TableLayout inside a ListView. What I want is to display each row with a different color (two colors alternatively). It works well when using a normal LinearLayout in the ListView, but for whatever reason when using a TableLayout I end up having all rows having the same background, which I think is the last one set.

For this, I am using a BaseAdapter, and in the getView function I have something like:

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list, parent, false);
                holder = new ViewHolder();
                holder.from = (TextView) convertView.findViewById(R.id.from);
                holder.to = (TextView) convertView.findViewById(R.id.to);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(position%2==0) {
                convertView.setBackgroundColor(R.color.cell_background);
            }
            //alternate background
            else {
                convertView.setBackgroundColor(R.color.cell_background_alternate);
            }

            // Bind the data efficiently with the holder.
            holder.from.setText(list.get(position)[0]);
            holder.to.setText(list.get(position)[1]);

            return convertView;
        }

Now in my main.xml for the activity, I just have a ListView. In the list.xml used to inflate the list, I have:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/from"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>

        <TextView android:id="@+id/to"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"/>
    </TableRow>
</TableLayout>

Also, another problem is that the columns are not aligned: it just takes as much space as it needs for every row, every columns, when a TableLayout should align all columns. The only workaround that I came up with was to set a minimal width for the columns, but that's not pretty. Any reason why it does not work here?

Edit 21/06: cell not aligned

As you can see on the picture, the columns are not aligned (row 2 and 3 are but it's obviously because they have the same data inside).

2 Answers 2

0

try replacing

convertView = mInflater.inflate(R.layout.list, parent, false);

with

convertView = mInflater.inflate(R.layout.list, null);

and for table cell aligning issue can you give an example snapshot bcos I did not get what u trying to accomplish.

3
  • Thanks for your reply. I tried your way, but it didn't change anything. I added a screenshot of what it looks like: every row should have a different background and for example 4 should come under Results
    – Guillaume
    Jun 21, 2011 at 9:46
  • for table alignment try android:gravity="left" instead of android:layout_gravity="left". And for coloring I don't know why its not working, can you try changing color of main table layout instead of complete view.
    – mkso
    Jun 21, 2011 at 14:03
  • android:gravity="left" does not change anything. What do you mean by changing the main table color? what I want is to have each row a different color, changing the color of the whole table won't help me much...
    – Guillaume
    Jun 22, 2011 at 4:37
0

I have also faced same problem of alignment of table header and table row having data but after lot of efforts i found the only way to get rid of this problem is to give fix size to all the columns

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.