Android ListView Adapter how to detect an empty list?

26,810

Solution 1

Use the method setEmptyView which sets the view to show if the adapter is empty.

Solution 2

The above answer sounds fine, but for the record the reason your method doesn't appear to be called is probably because your implementation of getCount() returns something like data.size() - meaning 0. This means the ListView will never call getView at all.

Instead you could do something like this:

public int getCount() {
    if (data.size()==0) {
        return 1;
    }
return data.size();
}

I hope that's helpful.

Solution 3

You are calling this adapter in some activity. there the adapter is set to any listview. Before setting the adapter to the list view you can check what is the size of arraylist. if the size is zero then you can add "No item inhere" message to the arraylist by make a hashmap

Share:
26,810
Weber Antoine
Author by

Weber Antoine

Updated on July 09, 2022

Comments

  • Weber Antoine
    Weber Antoine almost 2 years

    I have a ListView in my Activity which takes values from an extension class of SimpleAdapter. This one is correctly working when I add or delete some data in it.

    But my problem is, when my item list is empty, I would like to replace the listView by a message like "No item inhere" or something like that.

    I tried to do it, this way:

    class FavAdapter extends SimpleAdapter{
        Activity context;
        ArrayList<HashMap<String,String>> data;
        String[] items;
        int[] champs;
        int layout;
    
        FavAdapter(Activity _context, ArrayList<HashMap<String,String>> _data, int _layout, String[] _items, int[] _champs){
            super(_context, _data, _layout, _items, _champs );
            this.data = _data;
            this.context = _context;
            this.items = _items;
            this.layout = _layout;
            this.champs = _champs;
        }
    
        public View getView(int pos, View convertView, ViewGroup parent){
            //Recycling
            View row = convertView;
            //Else get from XML
            if(row == null){
                LayoutInflater infla = context.getLayoutInflater();
                row = infla.inflate(layout, null);
            }
    
            //If there are data
            if(data.size() > 0){
                /**** Here I am correctly constructing row  *****/
                 ...
            }
            //Else
            else{
                TextView txt = new TextView(ActivityFavoris.this);
                txt.setText("Not data inhere");
                LinearLayout ll = (LinearLayout) row.findViewById(R.id.result_lyt_principal);
                ll.addView(txt);
            }
            return row;
        }
    

    Problem is: my list has never a size of 0. in fact when the data is empty the adapter is not called.

    Can someone explain me how I can do it by another way? Thanks in advance.

    Edit : Using your answers I succeed on that. My solution is : setContentView has to be used with findViewById(android.R.id.empty), but if like me you have two different listView in your activity, which need different empty views, you will have to find it inside different layout. This way android will automatically call the right view when the listview is empty, and nothing have to be done in the adapter when data.size is 0.

    example: in Activity :

    public void onCreate(Bundle b){
        super.onCreate(b);
        lv1 = (ListView) findViewById(R.id.listviewone);
        lv2 = (ListView) findViewById(R.id.listviewtwo);
        //****set listview adater***//
        ...
        //set empty view
        lv1.setEmptyView(findViewById(R.id.layoutone).findViewById(android.R.id.empty));
        lv2.setEmptyView(findViewById(R.id.layouttwo).findViewById(android.R.id.empty));
    }
    

    with that xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
    <LinearLayout
        android:id="@+id/layoutone"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ListView
            android:id="@+id/listviewone"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <TextView 
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Pas de favoris enregistrées" />
    
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/layouttwo"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ListView
            android:id="@+id/listviewtwo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
        <TextView 
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Pas de favoris enregistrées" />
    
    </LinearLayout>