I'm deleting the ListView item and it works fine, except that the notifyDataSetChanged () method; it is not working in my ListView and the item keeps popping up even after delete. What can I do in this case?
class that creates the adapter
public class ListaNotificacoes extends Fragment{
View minha_view;
public ArrayList<ItensDaLista> lista_menu;
public ListView lista_notify;
public static ItensDoAdaptador itens_adaptador;
@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");
lista_menu = new ArrayList<>();
itens_adaptador = new ItensDoAdaptador(getActivity(), lista_menu);
lista_notify = (ListView)getActivity().findViewById(R.id.list_notify);
lista_notify.setAdapter(itens_adaptador);
//lista_notify.setEmptyView(getActivity().findViewById(R.id.empty_view));
//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();
}
}
}
class that is executing the delete method
public class ItensDoAdaptador extends ArrayAdapter<ItensDaLista> {
public ItensDoAdaptador(Activity context, ArrayList<ItensDaLista> iten_drawer) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context,0, iten_drawer);
}
public ItensDoAdaptador(Activity context, int count){
super(context,0,count);
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item_notify, parent, false);
}
final ItensDaLista itens_adapter = getItem(position);
TextView imagem_drawer = (TextView) listItemView.findViewById(R.id.numero_notificacao);
imagem_drawer.setText(itens_adapter.getNumero_notificacao());
ImageView imagem_drawer_2 = (ImageView) listItemView.findViewById(R.id.botao_deletar);
imagem_drawer_2.setImageResource(itens_adapter.getId_imagem());
imagem_drawer_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remover_tarefa(valueOf(itens_adapter.getNumero_notificacao()));
itens_adaptador.notifyDataSetChanged();
}
});
TextView texto_notify = (TextView) listItemView.findViewById(R.id.texto_notificacao_list);
texto_notify.setText(itens_adapter.getTexto());
TextView hora_notify = (TextView) listItemView.findViewById(R.id.texto_hora_list);
hora_notify.setText("Hora: " + itens_adapter.getHora());
return listItemView;
}
private void remover_tarefa(Integer id){
try {
banco_dados.execSQL("DELETE FROM lista_notificacoes WHERE id="+id);
}catch (Exception e){
e.printStackTrace();
}
}
}