0

I have an Activity extends ListActivity, where I had to create a ListView programmatically (no xml).

To set up an emptyView for it the only way that worked was the following code I found in another thread:

    TextView emptyView = new TextView(this);
    ((ViewGroup) getListView().getParent()).addView(emptyView);
    getListView().setEmptyView(emptyView);

now my goal is to do the same using a layout instead of a TextView, so I made an xml with its id and put it this way:

    View emptyView = findViewById(R.id.empty_view_xml);
    ((ViewGroup) getListView().getParent()).addView(emptyView);
    getListView().setEmptyView(emptyView);

but it doesn't seem to work..

1 Answer 1

1

Your problem:

Because your view (empty_view_xml) doesn't exist in the activity xml, so calling findViewById(R.id.empty_view_xml); will return a null value, and setting null as emptyView will do nothing.

Your current code: .setEmptyView(emptyView); is similar to .setEmptyView(null);.

To solve this:

You need to inflate your custom xml, then add it as EmptyView:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);
View emptyView = inflater.inflate(R.layout.empty_view_xml, null, false);
((ViewGroup) getListView().getParent()).addView(emptyView);
getListView().setEmptyView(emptyView);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.