Want to setEmptyView() of a ListActivity

后端 未结 1 1104
盖世英雄少女心
盖世英雄少女心 2021-01-24 23:37

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(\"N         


        
相关标签:
1条回答
  • 2021-01-25 00:39

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

    Now you'll see it!


    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题