Update: I am sorry i pasted in list.xml twice by mistake, my question is now correct!…
I created a class which extends ArrayAdapter, in which I override getView, and in there I capture controls in my row.xml, and setText on them appropiately with my values. This all works fine, I got no problem here.
What I then tried to do is have my output in tabular format, so I can span columns etc and get them all aligned as I wish. Here start my problem.
So I lave my two xml files, one for my list, and one for the row. In the list I define a TableLayout, with nothing in it except my ListView. Not how I set my stretchColumns property? Then in row.xml, I just define a TableRow, so I would expect to get a load of TableRows tags created inbetween the TableLayout start and end tags, and everything will turn out good.
Well it does work in the sense that I can see teh results, HOWEVER all of the TableRows are not aligned with the rest of thd table, or other rows. They just all get sixed to fit there own contents, and seems they are not properly a part of my TableLayout at all.
If I create by hand the xml I would EXPECT to be inflated in my custom adapter, and put it between the TableLayout tags (and remove the ListView) everything displays perfectly, columns aligned etc.
Perhaps I am doing something wrong, or perhaps I should not be using a TableLayout this way and ought to be doing things a different way.
Really appreciate any helps with this, been stuck puzzled on if for ages.
this is row.xml
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="QTY"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPartName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dip"
android:text="Part desererregre"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvPartCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingLeft="10dip"
android:text="PART CODE"
android:textSize="12dp"
android:textStyle="bold" />
</TableRow>
this is my list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/addCallPart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:stretchColumns="1" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</TableLayout>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="There are no records." />
</LinearLayout>
here is my code, this all works ok, but i include it to make the question clearer
public class CallPartAdapter extends ArrayAdapter<CallPart> {
private final Context context;
private final List<CallPart> values;
public CallPartAdapter(Context context, List<CallPart> values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.tvQty);
TextView textView2 = (TextView) rowView.findViewById(R.id.tvPartName);
TextView textView3 = (TextView) rowView.findViewById(R.id.tvPartCode);
textView1.setText(Integer.toString(values.get(position).Qty));
textView2.setText(values.get(position).PartName);
textView3.setText(values.get(position).PartCode);
return rowView;
}
}
You can’t use
TableLayoutthat way. Well, let me rephrase… you can but you won’t get the results you desire. You’ve essentially got aTableLayoutwith a single row: theListView. So none of the items in yourListViewwill receive any of the layout benefit of being in aTableLayout. They are simplyListViewitems.You have two real options:
List.TableLayoutstyle of layout and stick with theListViewparent.