2012-02-13 57 views
1

更新:对不起错两次我在list.xml粘贴,我的问题现在是正确的......如何从自定义ListView适配器正确格式化TableLayout?

我创建了一个类延伸ArrayAdapter,在我重写getView,并在那里我拍摄控制!在我的row.xml中,并在他们的setText与我的价值观相适应。这一切都很好,我在这里没有问题。

然后我试图做的是以表格格式输出,所以我可以跨栏等,让我们所有的对齐,如我所愿。这里开始我的问题。

因此,我准备了两个xml文件,一个用于列表,另一个用于行。在列表中我定义了一个TableLayout,除了我的ListView外没有任何内容。不是我如何设置我的stretchColumns属性?然后在row.xml中,我只是定义了一个TableRow,所以我希望能够在TableLayout的开始和结束标签之间创建一个TableRows标签,并且一切都会变好。

那么它的确能工作,我可以看到结果,但是所有TableRows都不与其他表格或其他行对齐。他们只是为了适应自己的内容而得到六分,看起来他们根本不属于我的TableLayout的一部分。

如果我用手XML我希望在我的自定义适配器进行充气,并把它的TableLayout标签之间(和删除ListView控件),一切显示完美打造,列排列等

也许我做错了什么,或者我不应该用这种方式使用TableLayout,而应该以不同的方式做事。

真的很感谢任何帮助,一直困惑,如果适合年龄。

这是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> 

这是我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> 

这里是我的代码,这一切工作正常,但我包括它使问题更清晰

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; 
    } 
} 

回答

0

您不能以这种方式使用TableLayout。好吧,让我换句话说......你可以但你不会得到你想要的结果。你基本上得到了一行TableLayoutListView。因此,您的ListView中的任何物品都不会收到TableLayout中的任何布局好处。他们只是ListView项目。

你有两个真正的选择:

  1. 动态的行添加到TableLayout。你仍然可以使用一个适配器,但它不会给你买得太多,因为你只是将它用作List
  2. 修改您的行布局以提供TableLayout布局样式并坚持使用ListView父级。
相关问题