3

I have an empty view which I set via the ListView object that I have created. The issue that I'm facing is that even when the list is populated, the empty view is still shown and not the populated list view (I've tried removing the empty view and it works correctly).

Here's the code for the xml layout of the activity where I have the ListView and empty view(they're in a relative layout):

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/requests_list"
        />
    <TextView
        android:id="@+id/no_requests_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No current requests"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:fontFamily="sans-serif-smallcaps"
        android:visibility="gone"
        />

Here's how I set the empty view:

View emptyView = findViewById(R.id.no_requests_textView);
        RequestAdapter requestAdapter = new RequestAdapter(this, requestDetailsArrayList);

        ListView requestsListView = (ListView) findViewById(R.id.requests_list);
        requestsListView.setAdapter(requestAdapter);
        requestsListView.setEmptyView(emptyView);

Does anyone know what the issue is?

Thanks in advance!

2 Answers2

1

try this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
<TextView
    android:id="@+id/no_requests_textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    android:text="no request" >
</TextView>

TextView emptyView = findViewById(R.id.no_requests_textView);
if(requestDetailsArrayList.size()==0)

{
    emptyView.setVisibility(View.VISIBLE);
    ListView.setVisibility(View.GONE)
}

else

{
    emptyView.setVisibility(View.GONE);
    ListView.setVisibility(View.VISIBLE)
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

What you need to do is remove the emptyview from the main layout and make it in different layout and add following lines:

 ViewGroup parentGroup = (ViewGroup) requestsListView.getParent();
    View empty = getActivity().getLayoutInflater().inflate(R.layout.empty_view,
            parentGroup,
            false);
    parentGroup.addView(empty);
    requestsListView.setEmptyView(empty);
nitinkumarp
  • 2,120
  • 1
  • 21
  • 30