I am populating a ListActivity's ListView using an ArrayAdapter extended to apply some conditions to the data it returns. It's possible for the adapter to return an empty set for several possible reasons and as those conditions are set by the user, I'd like to feed back info using setText() on the android:empty view. In the ListActivity (both before and after the setListAdapter) I've tried
TextView t = new TextView(this);
t.setText("HEY!");
getListView().setEmptyView(t);
and also
getListView().getEmptyView().setVisibi开发者_运维百科lity(View.GONE);
TextView t = new TextView(this);
t.setText("HEY!");
((ViewGroup)getListView().getParent()).addView(t);
getListView().setEmptyView(t);
but I only get the message set in the layout android:empty.
Thanks!
I'm surprised this has not come up and been answered already ... I think it quite useful. Didn't take much digging to find an answer:
MyAdapter mAdapter = new MyAdapter(this, R.layout.file_row, data);
setListAdapter(mAdapter);
ListView listView = (ListView)findViewById(android.R.id.list);
if (mShowOnlySomeSubset) {
listView.setEmptyView(findViewById(R.id.emptySubset));
} else if (mShowOnlySomeOtherSubset) {
listView.setEmptyView(findViewById(R.id.emptyOtherSubset));
} else {
listView.setEmptyView(findViewById(android.R.id.empty));
}
R.id.emptySubset and R.id.emptyOtherSubset are the id's of TextViews defined in the same layout .xml file as the android:list and android:empty elements.
Easy peasy :)
You might try this:
http://www.littlefluffytoys.com/?p=74
Basically they say that you have to make sure to add the emptyView to the view hierarchy before using it.
TextView emptyView = new TextView(context);
emptyView.setLayoutParams(
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setText(“This appears when the list is empty”);
emptyView.setVisibility(View.GONE);
((ViewGroup)list.getParent()).addView(emptyView);
list.setEmptyView(emptyView);
You could take this slightly different approach: http://wiresareobsolete.com/wordpress/2010/08/adapting-to-empty-views/
精彩评论