Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView and ArrayAdapter<String>

Tags:

arrays

android

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"
/>
like image 456
STeN Avatar asked Jul 01 '10 11:07

STeN


1 Answers

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.

like image 166
erdomester Avatar answered Nov 09 '22 03:11

erdomester