views:

321

answers:

5

I've created custom ListView programmatically, and set view to show when ListAdapter is empty but nothing shows on the screen in that case, what can be wrong?

    public PlayerList(Context context, Activity activity) {
    super(context);
    mParent = activity;
    setOnItemClickListener(
        new OnItemClickListener() {    
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {   ...   } };

    List<PlayerInfo> players;
    players = getPlayerList(); // here size of players == 0
    setAdapter(new PlayersAdapter(this.getContext(), R.id.player_name, players));

    LayoutInflater vi = (LayoutInflater)mParent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    emptyView = vi.inflate(R.layout.empty_view, null); // emptyView is View, and member of PlayerList

    this.setEmptyView(emptyView);
}
A: 

hi.. I don't know if i understand your question.. But if your list is empty and you setted layout parameters width and height to wrap_content maybe you can just see your list cause it has 0 size. Could you show us your layout?

hara
when data exists everything is ok, i can see list properly
Yorick
I have custom ListView, it is created programmatically. In it's constructor I inflate emptyView from xml and call setEmptyView() to bind it to my custom ListView. Then I run my program: if data for my ListView exists, program shows it correctly, but when there is no data, then program shows empty screen. Why so?
Yorick
A: 

here is emptyView layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/empty_list">
<TextView android:id="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="No players detected..." 
android:textSize="30px" 
android:layout_gravity="center_horizontal"></TextView>
<TextView 
android:id="@+id/TextView02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></TextView>
<Button android:id="@+id/Button01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center_horizontal" 
android:text="Refresh"></Button>
</LinearLayout>
Yorick
A: 

here is full source:

project source

Yorick
A: 

you could try another way to inflate your view:

View.inflate(your_context, R.layout.empty_view, null);
Roflcoptr
I've tried - same result.
Yorick
A: 

I've changed the way of creating my custom ListView from creating it programmatically to inflating it from xml layout, and in that xml I've described my custom listview and LinearLayout with id android:empty.

Surprisingly if in my ListView's constructor I call setEmptyView(some_view), then in case of empty Adapter View with id = android:empty is shown, in other case list is shown correctly. If I don't call setEmptyView(some_view) in constructor, then in case of empty Adapter nohting is shown, probably emptyView is null.

Now it works.

Yorick
Oops... EmptyView was shown due to zero size of ListView. Finally I looked in ApiDemos - List8 and done all correctly. The error was that my ListView is created inside View.inflate(...), and in it's constructor setEmptyView(findViewById(...)) setted emptyView to null.
Yorick