2012-02-13 59 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父級。
相關問題