因此,我已經搜索了一下,並搜索了我的想法可能是一個荒謬的疏忽的答案,但這裏。Android:ListView告訴我它已經填充,但沒有顯示項目
我有一個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>
(我設置的顏色,所以我可以看到發生了什麼更容易事)
的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上,並試了一下。
當我更改ListView
對象android:layout_height
在我的XML來wrap_content
,我只看到不透明的背景是在包裝它LinearLayout
。當我將ListView
android:layout_height
設置爲match_parent
時,整個屏幕是#206
(magentish)。
當我在適配器上檢查getCount()
之前setAdapter()
它說有項目。當我從視圖本身檢查它時,它說有相同數量的項目。我完全失去了這個,但沒有顯示。
我剛剛檢查過,它工作正常。我懷疑在調用'buildPlaylistView();'並且有效地重置其內容和/或適配器之後,其他的東西正在搞亂'playlistView'。這只是不在你發佈的代碼中。 – andr
我對playlistView global做的唯一事情就是調用bringToFront()並將它評論出來。 我還設置了可見性,無效以強制重繪並在連接到id/playlist_screen(外部佈局)的視圖上執行一些動畫。 – Kimo
好吧......我不知道。如果可以的話,上傳整個項目。沒有它只是猜測。就像我說的 - 我嘲笑'playlist.titlesAsArray()'返回靜態數據,在實際的設備上運行它,並罰款。通過罰款我的意思是它顯示適配器中的所有項目與適當的顏色等您還可以提供更多的提示 - 你使用的是什麼API?你在模擬器或設備上運行這個嗎?在這裏一切都很重要...... – andr