Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView Adapter how to detect an empty list?

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>

like image 586
Weber Antoine Avatar asked Jun 30 '11 12:06

Weber Antoine


People also ask

How do I check if a list is empty?

just check if (cartlist. size()<0) then yours list is Empty.!

How do you display the data from the database in the ListView in android studio?

xml. Create an another layout file (list_row. xml) in /res/layout folder to show the data in listview, for that right click on layout folder à add new Layout resource file à Give name as list_row. xml and write the code like as shown below.

Is List View deprecated Android?

Warning:` ListView is deprecated and will be removed in a future release.


1 Answers

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

like image 173
xevincent Avatar answered Nov 15 '22 08:11

xevincent