【发布时间】:2010-11-27 17:45:49
【问题描述】:
我有一个以 XML 定义的 LinearLayout,我想重复使用它来显示列表的元素。 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,现在视图正确膨胀了。现在的问题是将文本动态添加到 TextViews。我试试这个:
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);
它以某种方式起作用(即使事件以相反的顺序显示)。有人知道这里发生了什么吗?
【问题讨论】: