STeN
STeN

Reputation: 73

ListView and ArrayAdapter<String>

Is there anything bad with this code?

The thing is that the "test" is not displayed in 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:

<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

Upvotes: 5

Views: 36233

Answers (4)

Rishabh Srivastava
Rishabh Srivastava

Reputation: 3745

Try android:layout_height="match_parent" in the ListView

Upvotes: 0

Jon
Jon

Reputation: 1388

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

Upvotes: 0

erdomester
erdomester

Reputation: 11829

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.

Upvotes: 8

David Hedlund
David Hedlund

Reputation: 129832

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.

Upvotes: 0

Related Questions