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();
}
}
}
But if it is in the xml of listview it will appear even without being empty
– Demetrius
No, it doesn’t. Look at this reply.
– ramaral
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.
– ramaral
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 ?
– Demetrius