2013-02-05 64 views
5

因此,我已經搜索了一下,並搜索了我的想法可能是一個荒謬的疏忽的答案,但這裏。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。當我將ListViewandroid:layout_height設置爲match_parent時,整個屏幕是#206(magentish)。

當我在適配器上檢查getCount()之前setAdapter()它說有項目。當我從視圖本身檢查它時,它說有相同數量的項目。我完全失去了這個,但沒有顯示。

+0

我剛剛檢查過,它工作正常。我懷疑在調用'buildPlaylistView();'並且有效地重置其內容和/或適配器之後,其他的東西正在搞亂'playlistView'。這只是不在你發佈的代碼中。 – andr

+0

我對playlistView global做的唯一事情就是調用bringToFront()並將它評論出來。 我還設置了可見性,無效以強制重繪並在連接到id/playlist_screen(外部佈局)的視圖上執行一些動畫。 – Kimo

+0

好吧......我不知道。如果可以的話,上傳整個項目。沒有它只是猜測。就像我說的 - 我嘲笑'playlist.titlesAsArray()'返回靜態數據,在實際的設備上運行它,並罰款。通過罰款我的意思是它顯示適配器中的所有項目與適當的顏色等您還可以提供更多的提示 - 你使用的是什麼API?你在模擬器或設備上運行這個嗎?在這裏一切都很重要...... – andr

回答

2

嘗試更改從android:orientation="horizontal"android:orientation="vertical"這可能會解決。

+0

沒有工作,但導致我問,可以ListView的不水平?我的應用程序的其餘部分是水平的,這就是爲什麼那樣(它以橫向格式播放視頻) – Kimo

+0

對於列表視圖,我們不能設置方向,在我提到有關父視圖方向的文章中。我懷疑對象正在渲染,但由於佈局隱藏。 –

+0

謝謝你,我已經刪除了方向(並嘗試了其他一些東西) – Kimo

相關問題