要setEmptyView()的ListActivity [英] Want to setEmptyView() of a ListActivity

查看:72
本文介绍了要setEmptyView()的ListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我想为列表活动设置默认视图.我试图做到这一点:

As the title suggests, I want to set a default view for a list activity. I have tried to do this :

TextView emptyView = new TextView(this);
emptyView.setText("No lists available");
this.getListView().setEmptyView(emptyView);

但这没用.

推荐答案

问题是,如果使用addView(),则emptyView永远不会附加到任何东西:

The problem is that emptyView is never attached to anything, if you use addView():

TextView emptyView = new TextView(this);
((ViewGroup) getListView().getParent()).addView(emptyView);
emptyView.setText("It's empty!");
getListView().setEmptyView(emptyView);

现在您将看到它!

我写了一个快速的Runnable来在空/满"之间切换...

I wrote a quick Runnable to alternate between empty / "full"...

public class Example extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView emptyView = new TextView(this);
        ((ViewGroup) getListView().getParent()).addView(emptyView);
        emptyView.setText("It's empty!");
        getListView().setEmptyView(emptyView);

        getListView().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(getListAdapter() == null)
                    setListAdapter(new ArrayAdapter<String>(Example.this, android.R.layout.simple_list_item_1, new String[] {"It", "Has", "Content"}));
                else
                    setListAdapter(null);
                getListView().postDelayed(this, 2000);
            }
        }, 2000);
    }
}

这篇关于要setEmptyView()的ListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆