0

Basically I have a ListView (lv) that displays a bunch of items, if the lv is empty then display the EmptyView, but it displays nothing Code:

ListView l = (ListView) view.findViewById(R.id.ArrayList);
    l.setAdapter(adapter);
    srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView);
    srl.setOnRefreshListener(srfListener());
    View Empty = inflater.inflate(R.layout.empty,null);
    l.setEmptyView(Empty);

XML File:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:text="Nothing Available "
    android:textSize="30sp"
    android:textColor="@color/colorPrimary"
    android:layout_height="match_parent"
    >
</TextView>

Any Ideas to fix this?? By the way, its not a list activity.

DEVANDRIN KUNI
  • 81
  • 1
  • 2
  • 11

1 Answers1

1

As per you code, You are creating the view and adding as emptyView in list,

View Empty = inflater.inflate(R.layout.empty,null);
l.setEmptyView(Empty);

But you can see actual code below its just making VISIBLE and GONE, Its not adding into your layout.

private void updateEmptyStatus(boolean empty) {
        if (isInFilterMode()) {
            empty = false;
        }

        if (empty) {
            if (mEmptyView != null) {
                mEmptyView.setVisibility(View.VISIBLE);
                setVisibility(View.GONE);
            } else {
                // If the caller just removed our empty view, make sure the list view is visible
                setVisibility(View.VISIBLE);
            }

            // We are now GONE, so pending layouts will not be dispatched.
            // Force one here to make sure that the state of the list matches
            // the state of the adapter.
            if (mDataChanged) {           
                this.onLayout(false, mLeft, mTop, mRight, mBottom); 
            }
        } else {
            if (mEmptyView != null) mEmptyView.setVisibility(View.GONE);
            setVisibility(View.VISIBLE);
        }
    }

So add it your layout and then give that view to setEmptyView.

You can add like this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:swipe="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:background="@color/dark_bg_color"
    android:gravity="top"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/recentcall_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@color/transparent"
        android:divider="#cecece"
        android:dividerHeight="1dp"
        android:listSelector="@color/transparent"
         />

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_margin="20dp"
        android:layout="@layout/[your empty layout]" />

</LinearLayout>

And in your java code, you can call like this,

ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty);
l.setEmptyView(emptyViewStub);
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41