Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty view cannot be set while there is no items in the list #13

Open
suji099 opened this issue Aug 20, 2012 · 7 comments
Open

Empty view cannot be set while there is no items in the list #13

suji099 opened this issue Aug 20, 2012 · 7 comments

Comments

@suji099
Copy link

suji099 commented Aug 20, 2012

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 .

@erikwt
Copy link
Owner

erikwt commented Sep 20, 2012

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.

@erikwt erikwt closed this as completed Sep 20, 2012
@vincentjames501
Copy link

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

@erikwt
Copy link
Owner

erikwt commented Sep 25, 2012

Yes, you are right. I'm re-opening the issue and will look into it ASAP.

@erikwt erikwt reopened this Sep 25, 2012
@vincentjames501
Copy link

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

@tagrudev
Copy link

a pull request >?

@erikwt
Copy link
Owner

erikwt commented Oct 25, 2012

A pull request would be nice...

@Joohansson
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants