所以我用谷歌搜索,并搜索SO以寻找我认为可能是一个荒谬的疏忽的答案,但是这里有.
我有一个ListView,我正在填充一个ArrayAdapter我正在我的应用程序中的其他地方使用的对象列表.我已经检查getCount过,在我调用之前和之后,适配器中都有项目.setAdapter().但是,我的应用程序中没有任何内容出现.
我的主要布局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>
Run Code Online (Sandbox Code Playgroud)
(我设置了颜色,这样我就可以更容易地看到发生了什么)
在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>
Run Code Online (Sandbox Code Playgroud)
和我用来填充它的代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
playlist.titlesAsArray()确实返回String[]并且有效.
我有两个.notifyDataSetChanged() …