-1

I'm trying to show a listView after i deleted some items. But, after i deleted some items, OnItemClick crash (java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification). I want to show a new list after i deleted, like a filter. I already try to use adapter.remove(position) and list.remove(position) like the example down.

My OnItemClick


                //listView.invalidateViews();
                //listView.refreshDrawableState();
                //pullToRefresh.setRefreshing(false);
                Collections.sort(lista, new Viagem());
                adapter.notifyDataSetChanged();
                System.out.println(position);
                Viagem vg = (Viagem)  parent.getItemAtPosition(position);

                Bundle bundle = new Bundle();
                bundle.putString("usuario", usuario);
                bundle.putString("usuarioViagem", vg.getUsuario());
                bundle.putString("data", vg.getData());
                bundle.putString("hora", vg.getHora());
                Intent intent = new Intent(getApplicationContext(), MostraInfoViagemActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

How i deleted some items.

public void delete(){
                    jaEntrou = false;
                    ArrayList <Integer> apagar = new ArrayList<>();
                    Viagem aux;
                    List<String> listaUsuarios;
                    for(int i = 0 ; i < lista.size(); i++){
                        jaEntrou = false;
                        aux = lista.get(i);
                        listaUsuarios = aux.getUsuarios();
                        for(int j = 0; j < listaUsuarios.size(); j++){
                            if(listaUsuarios.get(j).equals(usuario)){
                                jaEntrou = true;
                            }
                        }
                        if(!jaEntrou){
                            apagar.add(i);
                        }
                    }
                    for(int i = apagar.size() - 1; i >= 0; i--){
                        int index = apagar.get(i);
                        lista.remove(index);
                        //adapter.removeItem(apagar.get(i));
                        //adapter.notifyDataSetChanged();
                    }
                    adapter.notifyDataSetChanged();
                    listView.invalidateViews();
                    listView.refreshDrawableState();
                    lista = fb.getLista();
                    Intent intent = new Intent(getApplicationContext(), OnlyDelay.class);
                    startActivity(intent);
}

My Custom List

package com.example.caronassuldeminas;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class Custom_List extends BaseAdapter {
    private List<Viagem> viagens;
    private Activity act;
    private Object orig;
    public Custom_List(List<Viagem> viagens, Activity act){
        this.viagens = viagens;
        this.act = act;

    }
    @Override
    public int getCount() {
        return viagens.size();
    }

    @Override
    public Object getItem(int position) {
        return viagens.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void removeItem(int position){
        viagens.remove(position);
    }

    public void attLista(List<Viagem> viagens){
        this.viagens = viagens;
    }

    public Viagem getViagem(int position){
        return viagens.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = act.getLayoutInflater().inflate(R.layout.list_item, parent, false);

        TextView cidadePartida = (TextView) view.findViewById(R.id.textView8);
        TextView cidadeChegada = (TextView) view.findViewById(R.id.textView9);
        TextView dia = (TextView) view.findViewById(R.id.textView10);
        TextView hora = (TextView) view.findViewById(R.id.textView11);
        cidadePartida.setText(viagens.get(position).getCidadePartida());
        cidadeChegada.setText(viagens.get(position).getCidadeChegada());
        dia.setText("Dia: " + viagens.get(position).getData());
        hora.setText("Hora: " + padronizaHora(viagens.get(position).getHora()) + "h");

        return view;
    }
    public String padronizaHora(String data){
        if(data.length() == 5)
            return data;
        int pos = 0;
        String resu = "";
        for(int i = 0; i < data.length(); i++){
            if(data.charAt(i) == ':'){
                pos = i;
                break;
            }
        }
        if(pos < 2){
            resu += "0";
        }
        resu += data.substring(0, pos);
        resu += ":";
        if(data.charAt(data.length()-1) == '0'){
            resu += "0";
        }
        resu += data.substring(pos + 1);
        return resu;
    }
}

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • post your crash log – hemen Jun 06 '19 at 15:31
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Jun 06 '19 at 15:37

1 Answers1

-2

Usually, when I add or remove items from the ArrayList that drives the ListView and Adapter, I recreate the adapter and re-assign it to the ListView rather than call notifyDatasetChanged. This also seems to prevent any crashes, which most likely are caused by the adapter trying to reference ArrayList elements that are no longer there.

    adapter = new TripSelectAdapter(this, thisContext, tripItems);
    mTripList.setAdapter(adapter);
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13