所以我用谷歌搜索并搜索了 SO 以寻找我认为可能是一个荒谬的疏忽的答案,但这里是。
我有一个 ListView
,我正在用一个 ArrayAdapter
填充它,我正在从我在应用程序其他地方使用的对象列表中构建它。在我调用 .setAdapter()
之前和之后,我已经通过 getCount
检查适配器中是否有项目。但是,我的应用程序中没有显示任何内容。
我的主要布局res/layout/playlistview
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/playlist_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#80000000"
android:gravity="center"
android:orientation="horizontal" >
<ListView
android:id="@+id/playlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#206"
android:background="#602" >
</ListView>
</LinearLayout>
(我设置了颜色,这样我可以更容易地看到发生了什么)
每个项目的textview
res/layout/singlelistitem
:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="5dp"
android:background="#206">
</TextView>
以及我用来填充它的代码:
private ListView playlistView;
private void buildPlaylistView() {
playlistView = (ListView)findViewById(R.id.playlistview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlelistitem, R.id.single_item, playlist.titlesAsArray());
playlistView.setAdapter(adapter);
playlistView.setVisibility(View.VISIBLE);
adapter.notifyDataSetChanged();
((ArrayAdapter<String>)playlistView.getAdapter()).notifyDataSetChanged();
}
playlist.titlesAsArray()
确实返回 String[]
并且有效。
我有两个 .notifyDataSetChanged()
,因为我在 SO 上找到了它并试了一下。
当我将 XML 中 ListView
对象中的 android:layout_height
更改为 wrap_content
时,我只能看到不透明的背景包装它的 LinearLayout
。当我将 ListView
android:layout_height
设置为 match_parent
时,整个屏幕都是 #206
(magentish)。
当我在 setAdapter()
之前检查适配器上的 getCount()
时,它说有项目。当我之后检查它时,从 View 本身来看,它说有相同数量的项目。我完全迷失了这一点,但没有显示任何内容。
最佳答案
尝试将 android:orientation="horizontal"
更改为 android:orientation="vertical"
这可能会解决。
关于java - Android:ListView 告诉我它已填充但未显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14698077/