AdapterView在无数据情况下的界面显示

在布局文件中将空界面布局与listview布局放在同一个framelayout下,通过adapterview的setEmptyView方法设置空数据时显示的布局

布局文件activity_foodlist.xml部分代码如下

...
                    android:layout_width="match_parent"
            android:layout_height="wrap_content" >

                            android:id="@+id/btn_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="开始" />

       

                    android:layout_width="match_parent"
            android:layout_height="wrap_content" >

                        android:id="@+id/tv_foodlistempty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="无菜单" />

                        android:id="@+id/lv_foodList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            

       
...

Activity中的部分实现代码

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

        mFoods = initData();

        Button btnStart    = (Button)findViewById(R.id.btn_start);
        ListView lvFoods = (ListView)findViewById(R.id.lv_foodList);
        TextView tvEmpty    = (TextView)findViewById(R.id.tv_foodlistempty);
        EditText etMainFoodCount = (EditText)findViewById(R.id.et_mainFoodCount);
        etFoodCount = (EditText)findViewById(R.id.et_foodCount);
        lvFoods.setEmptyView(tvEmpty);
        getData(mFoods, mFoods.size());

        mViewAdapter = new ArrayAdapter(this,
                    R.layout.testmenu_list_item,     //listview需要一个布局文件来完成对item的布局
                    R.id.tv_testmenu_item,            //确认在item布局中用于显示内容的控件id
                    mFoodList);
        lvFoods.setAdapter(mViewAdapter);

        ...

}

当lv_foodList无数据时,显示tv_foodlistempty

你可能感兴趣的:(android,界面)