Insert image in empty listview background [duplicate]

0

I have a listview where it receives the data via Firebase. But if it is empty and not a white screen in the Activity where it is, I put a background image similar to the example below.

Thisismycodewhereyouareplacingtheimage.Itdoesatestifthesizeofthelistviewisequalto0,itdisplaysanImageView,otherwisethedata.

if(listOnibusAtrasado.size()==0){progressBar.setVisibility(View.INVISIBLE);arrayAdapterOnibusAtrasado.notifyDataSetChanged();listView_OnibusAtrasado.setAdapter(arrayAdapterOnibusAtrasado);imagem.setImageResource(R.drawable.ic_smile);txt_NenhumHorarioRelatado.setText("Nenhum Atraso Relatado pelos Usuários");

                    }else {

                        arrayAdapterOnibusAtrasado.sort(new Comparator<OnibusAtrasados>() {
                            @Override
                            public int compare(OnibusAtrasados o1, OnibusAtrasados o2) {

                                return o2.getHorário().compareTo(o1.getHorário());
                            }
                        });
                        arrayAdapterOnibusAtrasado.sort(new Comparator<OnibusAtrasados>() {
                            @Override
                            public int compare(OnibusAtrasados o1, OnibusAtrasados o2) {
                                return o2.getData().compareTo(o1.getData());
                            }
                        });

                        arrayAdapterOnibusAtrasado.notifyDataSetChanged();                       
                      listView_OnibusAtrasado.setAdapter(arrayAdapterOnibusAtrasado);
                        progressBar.setVisibility(View.INVISIBLE);
                    }

My question is, would that be the right way? For what I am doing is to take an image and a textview and put in that test (if else) that I quoted above.

    
asked by anonymous 02.09.2017 / 16:39

1 answer

1

You do not have to do any of this. Just use the setEmptyView() method of the ListView pointed to a View or ViewGroup any of the XML Layout and it will automatically load when the list is empty.

Ex:

ImageView emptyView  = findViewById(R.id.image_empty);
ListView list = findViewById(R.id.list_view);
list.setEmptyView(emptyView);
    
02.09.2017 / 21:17