1

I've got a ListView in my application that's rendered in a ListFragment's onActivityCreated()using setListAdapter(). Passed in to this setListAdapter()are my implementation of ArrayAdapter. At some times this adapter can be empty, and that's fine, but at those moments I would like to show a message in the list telling that there are no items, instead of just an empty view. However I don't really know how to achieve this, as for I have researched most people to show lists in a ListActivityand by doing that you can setEmptyView() but this doesn't seem doable when using a ListFragment. Another way of doing this was to change view in the ListFragment if the ArrayAdapter has no item's in it, then change view to another showing the message, but this seems to me a bit hacky.

Whats really the proper way of doing what I want to achieve?

Tried to setEmptyView() to my ListView but that didn't work either, see code on how views are inflated in my ListFragment:

public class MyFragment extends ListFragment {

    @SuppressLint("InflateParams")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ListView listView = (ListView) inflater.inflate(R.layout.my_list, null);
        listView.setEmptyView(inflater.inflate(R.layout.list_items_missing, null));
        return listView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        MyItemArrayAdapter adapter = new MyItemArrayAdapter(getActivity());

        // Populate adapter with items...

        setListAdapter(adapter);
    }
}

Shouldn't this result in the empty view beeing shown if no items exists in my adapter?

5
  • try putting background image for ListView inside xml. If no item, show background image, if there is item, make background color. It is how google message app does Commented Apr 9, 2015 at 10:42
  • 3
    ListActivity has no setEmptyView method. The method is part of ListView Commented Apr 9, 2015 at 10:44
  • However this has issues and will not always work. Commented Apr 9, 2015 at 10:46
  • stackoverflow.com/questions/3771568/… Commented Apr 9, 2015 at 10:46
  • See updated question on how I'm trying to setEmptyView() to my ListView Commented Apr 9, 2015 at 14:05

2 Answers 2

2

1) Try putting backgroundImage for ListView inside xml. If no item, show backgroundImage, if there is item, put background color.

2) You can also do what as @kgandroid suggested and here, setEmptyView() which you set custom view.

Example:

enter image description here

Another:

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

I'd like to use a background image for listview, but can't get the resolutions right for that image. It will always look different for different devices and there screen resolutions. I guess I need to create a nine patch image but this start to look overkill for tha things I want to achieve. Then I tried to setEmptyView instead but that didnt work either. See updated question for the code.
@Robert this guy used it in ListFragment: stackoverflow.com/a/15990955/3736955. Try TextView emptyView=(TextView)inflater.inflate(R.layout.list_items_missing, null), (ViewGroup) getListView().getParent()).addView(emptyView); listView.setEmptyView(emptyView);
that did acually work, must have been the ((ViewGroup) getListView().getParent()).addView(emptyView); that was missing
just for curiosity, how do you made the background images for the list views so they look good on most devices? I still think using background images are the cleanest solution.
I haven't tried myself, i saw them in apps. Probably using different resolutions of image (xxhdpi, xhdpi,hdpi,mdpi,ldpi...)
0

You can customize your adapter to show the layout R.layout.list_items_missing when (item == null) instead of inflating the normal custom list item layout that you implement.

Something like this:

public View getView(int position, View convertView, ViewGroup parent) 
{
   ViewHolder holder; //asuming you have this in your custom adapter
if (convertView == null) {

        holder = new ViewHolder();
  if(MyItemArrayList.get(position)!=null)
{
        convertView = this.inflater.inflate(R.layout.layout_list_item,
                parent, false);
else
{
convertView = this.inflater.inflate(R.layout.list_items_missing,
                    parent, false);

}
//the rest of the adapter logic
}

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.