0

我正在尝试将使用 ArrayAdapter 的文件列表显示到 ListView 中。据我所知,以下应该可以工作,但它只是让 ListView 为空。我没有使用 ListActivity。

爪哇

setContentView(R.layout.files);
findViewById(R.id.folder_use).setOnClickListener(this);
ListView view = (ListView)findViewById(R.id.files);
File[] files = Environment.getExternalStorageDirectory().listFiles();
Log.d(TAG, files.toString());
view.setAdapter(new ArrayAdapter<File>(this, R.layout.file_row, files));

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">

    <TextView android:id="@+id/local_instructions" android:text="@string/local_instructions"
        android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />

    <ListView android:id="@+id/files" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_below="@id/local_instructions"
        android:background="#ffffff" />

    <Button android:id="@+id/folder_use" android:text="@string/folder_use" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentBottom="true" />
</RelativeLayout>

layout.file_row

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" />

我错过了一些明显的东西吗?

谢谢

4

1 回答 1

1

您是否检查了hierarchyviewer 以查看列表的测量大小是多少,它有多少个孩子,以及他们的大小?

您告诉 ListView 它的高度应该是wrap_content- 这对于 ListView 来说毫无意义,因为它的内容可以任意大。您还告诉列表项fill_parent的高度 - 这对于可以滚动的容器来说毫无意义,因为父级实际上有无限的高度要填充。

于 2010-07-21T00:16:22.533 回答