Skip to content Skip to sidebar Skip to footer

Want To Setemptyview() Of A 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');

Solution 1:

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

TextViewemptyView=newTextView(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"...

publicclassExampleextendsListActivity {
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        getListView().postDelayed(newRunnable() {
            @Overridepublicvoidrun() {
                if(getListAdapter() == null)
                    setListAdapter(newArrayAdapter<String>(Example.this, android.R.layout.simple_list_item_1, newString[] {"It", "Has", "Content"}));
                elsesetListAdapter(null);
                getListView().postDelayed(this, 2000);
            }
        }, 2000);
    }
}

Post a Comment for "Want To Setemptyview() Of A Listactivity"