Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a layout to use as empty view for a ListView in case adapter has zero items in a Activity

Tags:

android

How to use a layout as empty view for a listview when the adapter has zero elements?

setEmptyView is not working with this code :

public class ZeroItemListActivity extends Activity {
    private ArrayList<String> listItems=new ArrayList<String>();
    private ListView mMyListView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mMyListView=(ListView) findViewById(R.id.MyListView);
        mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
        LayoutInflater inflator=getLayoutInflater();
        View emptyView=inflator.inflate(R.layout.empty_list_item, null);
        //Empty view is set here
        mMyListView.setEmptyView(emptyView);
    }
    public void addItem(View v){
        listItems.add("list Item");
        mMyListView.invalidate();
    }
}

Layouts used : empty_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <Button android:id="@+id/Button01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Empty List,Click to add items"></Button>
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ListView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/MyListView"></ListView>
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/AddItemsButton"
        android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>
like image 441
Suresh Avatar asked Nov 16 '10 20:11

Suresh


2 Answers

The easiest way to get this done is to put the empty view in the main.xml layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:text="@string/hello" />
        <ListView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/MyListView">
        </ListView>
      <!-- empty view -->  
      <LinearLayout android:id="@+id/emptyView"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content">

            <Button android:id="@+id/Button01" 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:text="Empty List,Click to add items">
            </Button>
       </LinearLayout>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/AddItemsButton"
        android:text="Add Items" 
        android:onClick="addItem"></Button>
   </LinearLayout>

Then, in your code, set your empty view like this:

mMyListView.setEmptyView(findViewById(R.id.emptyView));

The reason your code isn't working is that the ListView and Empty view need to be in the same view heirarchy. And your call to inflate pass null as the parent so they are in different heirarchies.

like image 64
Josh Clemm Avatar answered Sep 27 '22 20:09

Josh Clemm


The Android Dev tutorial for List Activity covers this topic: http://developer.android.com/reference/android/app/ListActivity.html

Basically have your class extend ListActivity. Set your list id to @id/android:listand the "empty view" id to @id/android:emptyin the same XML layout.

Hope this helps :)

like image 38
HappyGeisha Avatar answered Sep 27 '22 20:09

HappyGeisha