Android ListView Adapter如何检测空列表?

Web*_*ine 14 android listview adapter android-arrayadapter

我的Activity中有一个ListView,它从SimpleAdapter的扩展类中获取值.当我添加或删除其中的一些数据时,这个正常工作.

但我的问题是,当我的项目列表为空时,我想用"No item inhere"之类的消息替换listView或类似的东西.

我尝试这样做,这样:

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;
    }
Run Code Online (Sandbox Code Playgroud)

问题是:我的列表从未大小为0.实际上当数据为空时,不调用适配器.

有人能解释我怎么能以另一种方式做到这一点吗?提前致谢.

编辑: 使用你的答案我成功了.我的解决方案是:setContentView必须与findViewById(android.R.id.empty)一起使用,但是如果像我这样你的活动中有两个不同的listView,它需要不同的空视图,你必须在不同的布局中找到它.这样,当listview为空时,android将自动调用右视图,当data.size为0时,不需要在适配器中完成任何操作.

示例:在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));
}
Run Code Online (Sandbox Code Playgroud)

用那个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>
Run Code Online (Sandbox Code Playgroud)

xev*_*ent 15

使用方法setEmptyView设置视图以显示适配器是否为空.