Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ListView tells me it's populated but not displaying items

So I've googled, and scoured SO for an answer to what I think is probably a ridiculous oversight, but here goes.

I have a ListView that I'm populating with an ArrayAdapter that I'm building out of a list of objects I'm using elsewhere in my application. I have checked via getCount that there are items in the adapter, both before and after I call .setAdapter(). Nothing is showing up in my application however.

My main layout 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>

(I set the colors so I could see what's going on more easily)

the textview for each item 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>

and the code I use to populate it:

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() does return String[] and it works.

I have both of the .notifyDataSetChanged() in there, since I found that on SO and gave it a try.

When I change the android:layout_height in the ListView object in my XML to wrap_content, I see only the opaque background that is in the LinearLayout that wraps it. When I set the ListView android:layout_height to match_parent, the whole screen is #206 (magentish).

When i check getCount() on the adapter before setAdapter() it says there are items. When I check it after, from the view itself, it says there are the same number of items. I'm totally lost on this one, but nothing is displayed.

like image 470
Kimo Avatar asked Oct 22 '22 18:10

Kimo


1 Answers

Try changing from android:orientation="horizontal" to android:orientation="vertical" this may fix.

like image 154
Satish Mahendran Avatar answered Oct 29 '22 19:10

Satish Mahendran