2009-09-12 55 views
1

我有一個用XML定義的LinearLayout,我想重複使用它來顯示列表元素。在XML佈局看起來是這樣的:顯示基於XML的佈局和動態添加文本的問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/background" 
android:paddingBottom="5px" 
> 
<TextView 
android:id="@+id/destination" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="22dp" 
android:text="@string/test_destination" 
android:paddingLeft="5px" 
/> 

<TextView 
android:id="@+id/date" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="15dp" 
android:text="@string/test_date" 
android:paddingLeft="5px" 
/> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/info" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingLeft="5px" 
android:paddingTop="10px" 
> 
    <TableRow> 
     <TextView 
      android:id="@+id/reg_label" 
      android:gravity="right" 
      android:paddingRight="5px" 
      android:text="@string/reg_label" /> 
     <TextView 
      android:text="@string/test_registered"/> 
    </TableRow> 
    <TableRow> 
     <TextView 
      android:id="@+id/info_label" 
      android:gravity="right" 
      android:paddingRight="5px" 
      android:text="@string/info_label" /> 
     <TextView 
      android:text="@string/test_info"/> 
    </TableRow> 
</TableLayout> 
</LinearLayout> 

我收集關於從網頁的某些事件的信息,然後我想顯示使用每個事件的上述佈局列表中的所有事件。我目前的做法是將LinearLayout作爲父項,然後將每個事件添加爲一行。

問題一是將佈局添加到每個事件(事件的數量是變化的)。也就是說,我想動態擴大每個事件的佈局。我也不希望文本被硬編碼到佈局中(如上所述),而是在運行時添加。我不知道該怎麼做。我嘗試了類似下面的內容,但沒有取得任何成功。

LinearLayout eventView = (LinearLayout) findViewById(R.layout.event); 
parentView.addView(eventView); 

問題二然後將這些佈局添加到父視圖組並將其顯示在屏幕上。當我嘗試使用parent.addView(child)來執行此操作時,程序在運行時崩潰,我找不到原因。

這是很難描述的具體問題,因爲我是Android的GUI編程新手,我現在正在嘗試編程。無論如何,如果你能夠幫助我的一些問題,將不勝感激。

萊納斯

編輯:

感謝commonsware,現在的看法膨脹正常。現在的問題是動態地向TextView添加文本。我試試這個:

TextView dest = (TextView) findViewById(R.id.destination); 
dest.setText("myText"); 

才發現dest爲空。任何想法爲什麼以及如何解決這個問題?

編輯2:

我已經更加縮小了問題,但我真的不明白它的性質。這是故障的方法:

private void formatEvents(ArrayList<Event> events, LinearLayout parentView) { 
    Log.d(TAG, "formatEvents, size: " + events.size()); 
    for (Event event : events) { 

     LinearLayout eventView = new LinearLayout(this); 
     eventView = (LinearLayout) getLayoutInflater().inflate(R.layout.event, null); 
     parentView.addView(eventView); 

     TextView dest = (TextView) findViewById(R.id.destination); 
     TextView date = (TextView) findViewById(R.id.date); 
     TextView registered = (TextView) findViewById(R.id.registered); 
     TextView info = (TextView) findViewById(R.id.info); 
     if (dest == null) 
      Log.d(TAG, "null"); 
     else { 
      Log.d(TAG, "not null"); 
      dest.setText(event.getDestination()); 
      date.setText(event.getDate()); 
      registered.setText(event.getDriver() + " " + event.getPassengers()); 
      info.setText(event.getInfo()); 
     } 
    }  
} 

我得到null爲第一次迭代,但不是爲第二次。當我改變

parentView.addView(eventView); 

parentView.addView(eventView, 0); 

它在某種程度上工作(即使事件顯示以相反的順序)。任何人都知道這裏發生了什麼?

回答

1

是ID爲 「目的地」 屬於佈局 「事件」?

如果是這樣的話,

TextView dest = (TextView) findViewById(R.id.destination);
應該

TextView dest = (TextView) eventView.findViewById(R.id.destination); 
+0

完美,謝謝! – aspartame 2009-09-13 09:41:19

1

總的來說,我建議您使用ListView而不是LinearLayout作爲行的容器。

問題號碼之一是與 佈局添加到每個事件(的 事件的數量是變化的)。也就是說,我想 動態膨脹這個佈局爲 每個事件。

parentView.addView(getLayoutInflater().inflate(R.layout.this_is_my_row));