How to update Listview after deleting item?

Asked

Viewed 460 times

0

I am deleting the Listview item and it works normal, only the notifyDataSetChanged() method is not working in my Listview and the item keeps appearing even after deleting. What can I do in this case ?

class creating 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 exclusion 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();
    }
}

}

2 answers

0

public class ItensDoAdaptador extends ArrayAdapter<ItensDaLista> {

public ItensDoAdaptador(Activity context, ArrayList<ItensDaLista> ten_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()));

Last time I made a routine mistake, I was using Cursoradapter, and I only did the Req-Query of the Following Way.

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

changeCursor(cursor);

notifyDataSetChanged();

When we are inside an Adapter we don’t need an Object to call notifyDataSetChanged();

        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();
}
}

0

Your Adapter is connected to an Arraylist. notifyDataSetChanged() will only take effect if you remove an item from that Arraylist and then call the method. In your case, you are doing this after deleting lines from the database, which is not connected to the Adapter.

Browser other questions tagged

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