Method setEmptyView is not working on my listview

Asked

Viewed 68 times

0

I am setting the setEmptyView method in my listview but when it is empty it does not show the desired view, and also does not give any error in the log.

public class ListaNotificacoes extends Fragment{

View minha_view;

private ListView lista_notify;
private ImageView botao_deletar;
private ItensDoAdaptador itens_adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.lista_notificacoes, container, false);
    return minha_view;
}

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

    }

    public void recupera_tarefa(){

        try {
            Cursor cursor = banco_dados.rawQuery("SELECT * FROM lista_notificacoes order by id DESC", null);

            //recupera os ids da coluna
            int indiceColunaId = cursor.getColumnIndex("id");
            int indiceColunaTexto = cursor.getColumnIndex("texto_notificacao");
            int indiceColunaHora = cursor.getColumnIndex("hora_notificacao");

            final ArrayList<ItensDaLista> lista_menu = new ArrayList<>();

            itens_adapter = new ItensDoAdaptador(getActivity(), lista_menu);
            lista_notify = (ListView)getActivity().findViewById(R.id.list_notify);
            lista_notify.setAdapter(itens_adapter);

            lista_notify.setEmptyView(getActivity().getLayoutInflater().inflate(R.layout.empty_listview, null));

            //listar as notificações
            cursor.moveToFirst();
            while (cursor != null) {
                String teste = cursor.getString(indiceColunaId);
                lista_menu.add(new ItensDaLista(teste.toString(),cursor.getString(indiceColunaTexto),cursor.getString(indiceColunaHora),R.drawable.delete));
                cursor.moveToNext();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

1 answer

0

In order for Listview to control the View to display when it is empty it is necessary that it is at the same level in its hierarchy.

The way you’re doing it doesn’t happen.

Do so:

//inflate da empty_listview
View emptyView = getActivity().getLayoutInflater().inflate(R.layout.empty_listview, null);
//Adiciona-a na mesma hierarquia da ListView
((ViewGroup) lista_notify.getParent()).addView(emptyView);
//Informa a ListView que deve usá-la como empty view
lista_notify.setEmptyView(emptyView);

However, the simplest is this view be declared in xml at the same level as Listview, and get it with findViewById()

  • But if it is in the xml of listview it will appear even without being empty

  • No, it doesn’t. Look at this reply.

  • If I understand your code correctly, you are doing something "weird" which is that Fragment is filling out a existing Listview in Activity. This should be done in Activity and not in Fragment. Or pass Listview to Fragment.

  • I already managed to make it work setEmptyView only that it does not want to remove the view when it already has items in the list, you know why ?

Browser other questions tagged

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