2013-02-05 66 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

相关问题