Is there anything bad with this code?
The thing is that the "test" is not displayed in ListView
.
Java:
priva开发者_开发百科te 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:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
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.
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.
I'm guessing that's where your problem is.
If you are using a ListActivity or ListFragment you'll need to change your Listview's name to: android:id="@+id/android:list"
edit: wow, just realized this question is three years old
Try android:layout_height="match_parent"
in the ListView
精彩评论