Search code examples
javaandroidxmllistviewandroid-cursoradapter

emptyView always showing even if list is not empty


I wish someone could take a look on that and let me know what is wrong.this is my emptyView container in the activity_main

<ListView
        android:id="@+id/display_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/empty_shelter_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif-medium"
            android:paddingTop="16dp"
            android:text="@string/empty_view_title_text"
            android:textAppearance="?android:textAppearanceMedium" />

        <TextView
            android:id="@+id/empty_subtitle_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/empty_shelter_title"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif"
            android:paddingTop="8dp"
            android:text="@string/empty_view_sub_text"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#A2AAB0" />

    </RelativeLayout>

I have set emptyView in each fragment, and even though when items are showing in the list it is still showing the empty view fields.

//set empty activity
    ListView listView = rootView.findViewById(R.id.display_view);
    View emptyView = rootView.findViewById(R.id.empty_view);
    listView.setEmptyView(emptyView);
    //create a new cursor adapter
    mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
    listView.setAdapter(mCursorAdapter);

I will highly appreciate it if you can tell me what i did wrong.


Solution

  • Try hiding the empty_view in xml.

    <RelativeLayout
            android:id="@+id/empty_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_centerInParent="true">