Insert picture into empty listview background

Asked

Viewed 261 times

0

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

inserir a descrição da imagem aqui

That’s my code where you’re putting the image. It makes a test if the listview size is equal to 0, it displays an Imageview, otherwise the data.

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 ? Because what I’m doing is taking an image and a textview and putting in this test (if Else) that I quoted above.

1 answer

1

You don’t have to do any of this. Just use the method setEmptyView() of Listview pointed to a View or Viewgroup of any XML Layout and this will be automatically loaded when the list is empty.

Ex:

ImageView emptyView  = findViewById(R.id.image_empty);
ListView list = findViewById(R.id.list_view);
list.setEmptyView(emptyView);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.