您的位置:首页 > 移动开发 > Android开发

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

2016-11-05 18:31 190 查看
在布局文件中将空界面布局与listview布局放在同一个framelayout下,通过adapterview的setEmptyView方法设置空数据时显示的布局

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

...

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >

            <Button

                android:id="@+id/btn_start"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="开始" />

        </LinearLayout>

        <FrameLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >

            <TextView

            android:id="@+id/tv_foodlistempty"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="无菜单" />

            <ListView

            android:id="@+id/lv_foodList"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" >

            </ListView>

        </FrameLayout>

...

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<String>(this,

                    R.layout.testmenu_list_item,     //listview需要一个布局文件来完成对item的布局

                    R.id.tv_testmenu_item,            //确认在item布局中用于显示内容的控件id

                    mFoodList);

        lvFoods.setAdapter(mViewAdapter);

        ...

}

当lv_foodList无数据时,显示tv_foodlistempty
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 界面
相关文章推荐