Android ListView 和 ArrayAdapter<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3157157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListView and ArrayAdapter<String>
提问by STeN
Is there anything bad with this code?
这段代码有什么不好的吗?
The thing is that the "test" is not displayed in ListView
.
问题是“测试”没有显示在ListView
.
Java:
爪哇:
private ListView namesListView;
private ArrayList<String> names;
private ArrayAdapter<String> namesAA;
...
namesListView = (ListView)findViewById(R.id.list);
names = new ArrayList<String>();
names.clear();
names.add(0, "test");
namesAA = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, names );
namesListView.setAdapter(namesAA);
...
XML:
XML:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
回答by erdomester
Have you tried this?
你试过这个吗?
namesAA = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, android.R.id.text1, names );
By the way, why are you specifying the place of the item? You are clearing the arraylist so adding names.add("test");
is better, I think.
顺便问一下,为什么要指定项目的位置?names.add("test");
我认为您正在清除数组列表,因此添加更好。
回答by dxh
The first thing I pick up is that layout_height
is set to wrap_content
which will not allocate more height than it absolutely needs. This is sort of paradoxal for a scrolling view, because there's really no physical limit to how small it could be.
我拿起的第一件事layout_height
是设置为wrap_content
不会分配比绝对需要更多的高度。这对于滚动视图来说有点自相矛盾,因为对于它可以有多小实际上没有物理限制。
I'm guessing that's where your problem is.
我猜这就是你的问题所在。
回答by Jon
If you are using a ListActivity or ListFragment you'll need to change your Listview's name to: android:id="@+id/android:list"
如果您使用的是 ListActivity 或 ListFragment,则需要将 Listview 的名称更改为: android:id="@+id/android:list"
edit: wow, just realized this question is three years old
编辑:哇,刚刚意识到这个问题已经三年了
回答by Rishabh Srivastava
Try android:layout_height="match_parent"
in the ListView
android:layout_height="match_parent"
在 ListView 中尝试