Method setEmptyView is not working in my listview

0

I'm setting the setEmptyView method in my listview, but when it is empty it does not show the desired view, and it 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();
        }
    }
}
    
asked by anonymous 10.06.2017 / 22:11

1 answer

0

In order for the ListView to control the View to display when it is empty, it must be at the same level in the hierarchy.

The way you're doing this does not happen.

Do this:

//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 view being declared in xml at the same level as the ListView, and get it with findViewById()

    
10.06.2017 / 23:00