想要ListActivity的setEmptyView()

时间:2013-03-10 18:33:35

标签: android layout view

正如标题所示,我想为列表活动设置默认视图。我试过这样做:

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

但这不起作用。

1 个答案:

答案 0 :(得分:3)

问题是emptyView永远不会附加到任何内容,如果您使用addView()

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

现在你会看到它!


我写了一个快速的Runnable来替换空/“完整”......

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);
    }
}