Gionne Lapuz
Gionne Lapuz

Reputation: 548

SetEmptyView is not displaying textView

I have added the emptyView value into the main.xml, but when the listview is empty it does not show anything. I am using a custom listview But if I only call my textView like this

listView.setEmptyView(textEmpty); 

it would work, but I wanted to use a layout to put multiple items

Can anyone help me with my problem? Thanks in advance!

My Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_cart"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="com.example.gin.ordering.cart">


<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    ></ListView>

<!-- empty view -->
<LinearLayout android:id="@+id/emptyView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/textEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YOUR CART IS EMPTY!"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="center_horizontal"
        />

</LinearLayout>

<Button
    android:id="@+id/btnCheckout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CHECKOUT"
    android:layout_alignParentRight="true"
    android:layout_gravity="right"
    android:layout_below="@id/listView"
    />

</LinearLayout>

And this is my Main Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

ListCartAdapter adapter = new ListCartAdapter(cart.this, orderid, orderName, 
orderSize, orderQuantity);
listView.setAdapter(adapter);

listView.setEmptyView(findViewById(R.id.emptyView));
}

Upvotes: 0

Views: 164

Answers (2)

user7451086
user7451086

Reputation:

You mean ListView and ListItem (EmptyView) right?

You need - ListView xml, ListItem xml, ListItem class, ArrayAdaper class

Upvotes: 0

Ali Ahsan
Ali Ahsan

Reputation: 470

change your Main.xml

<LinearLayout        
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_cart"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="com.example.gin.ordering.cart">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"

></ListView>

<!-- empty view -->

<TextView
    android:id="@+id/textEmpty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="YOUR CART IS EMPTY!"
    android:textStyle="bold"
    android:visibility="gone"
    android:textColor="#000000"
    android:layout_gravity="center_horizontal"
    />

    </LinearLayout>

<Button
android:id="@+id/btnCheckout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHECKOUT"
android:layout_gravity="right"
/>

</LinearLayout>

And In OnCreate of Main Activity before setting adapter to listView:

ListView view = (ListView)findViewById(R.id.emptyView);
view.setEmptyView(findViewById(R.id.textEmpty));

Let me know if that's help

Upvotes: 1

Related Questions