Empty view cannot be set while there is no items in the list
When there is no items in the list view i have to set an emptyview .In the current code there is no option to set an empty view .
The PullToRefreshListView does not override #setEmptyView of ListView, so you can just use http://developer.android.com/reference/android/widget/AdapterView.html#setEmptyView(android.view.View) or set the empty view in XML like you would on any other regular ListView.
I have to agree that this is a bug. When using setEmptyView, you lose the functionality to pull to refresh when the list is indeed empty. Also it is very gltchy when refreshing (as the list goes empty for a brief second while updating and you see the empty view).
Yes, you are right. I'm re-opening the issue and will look into it ASAP.
I thought I'd just go ahead and put my solution:
In PullToRefreshListView add the following:
private TextView emptyView; private String noItemsText;
public void removeEmptyHeaderView() {
if(getHeaderViewsCount()!=1) {
removeHeaderView(emptyView);
}
}
//modify public void onRefreshComplete() { state = State.PULL_TO_REFRESH; if (emptyView != null) { if (getAdapter().isEmpty()) { if (getHeaderViewsCount() == 1) { emptyView.setHeight(getHeight()); emptyView.setText(noItemsText); addHeaderView(emptyView); } } else { if (getHeaderViewsCount() == 2) { removeHeaderView(emptyView); } } } resetHeader(); }
//in init() add noItemsText = getContext().getString(R.string.ptr_no_items_text); //modify string xml file
emptyView = buildDefaultEmptyView();
//add this method - this can be refactored to use styles private TextView buildDefaultEmptyView() { TextView emptyView = new TextView(getContext()); emptyView.setText(noItemsText); emptyView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, AbsListView.LayoutParams.FILL_PARENT)); emptyView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL); emptyView.setTextColor(Color.BLACK); emptyView.setTextSize(20); return emptyView; }
public void setNoItemsText(String noItemsText) { this.noItemsText = noItemsText; }
public void setEmtpyView(TextView view) { this.emptyView = view; }
This can be refactored but simply gives a default empty view with a modifiable TextView
a pull request >?
A pull request would be nice...
I need the setEmptyView functionality and would be really happy if it´s solved. The solution vincentjames501 provided works, but not initially. The list is empty from the beginning and the text is not shown until a refresh is done. I want to tell the user that he needs to refresh. I tried to call the onRefreshComplete() manually but it will not update the header for some reason?