EmptyView is not replaced by listView

I have an empty view that I have set through a ListView object that I have created. The problem I am facing is that even when the list is full, the empty view is still displayed and not the filled list (I tried to remove the empty view and it works correctly).

Here is the code for the layout xml of the activity where I have a ListView and an empty view (they are in the 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"
        />

      

This is 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 problem is?

Thanks in advance!

+3


source to share


2 answers


try it

<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)
}

      

+1


source


What you need to do is remove the empty view from the main layout and make it in another layout and add the following lines:



 ViewGroup parentGroup = (ViewGroup) requestsListView.getParent();
    View empty = getActivity().getLayoutInflater().inflate(R.layout.empty_view,
            parentGroup,
            false);
    parentGroup.addView(empty);
    requestsListView.setEmptyView(empty);

      

0


source







All Articles