Android - Custom ArrayAdapter problems with index

0

I'm trying to make a ListView with a% custom%. When I use the ArrayAdapter original everything works fine but when I update the ArrayList and use ArrayList to update the values of notifyDataSetChanged() the app stops working and returns the error: ListView

Here is the code

private void configuraPaisesAdapter() {

    paisesAdapter = new ArrayAdapter<PaisCodigo>(this, R.layout.layout_duas_linhas_cell, paisesFiltrado) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent){

            View row = convertView;

            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.layout_duas_linhas_cell, null);
            }

            PaisCodigo pais = paisesFiltrado.get(position);

            if(pais != null) {
                TextView layoutDuasLinhasLinha1 = (TextView) row.findViewById(R.id.layoutDuasLinhasLinha1);
                TextView layoutDuasLinhasLinha2 = (TextView) row.findViewById(R.id.layoutDuasLinhasLinha2);
                ImageView layoutDuasLinhasCheckMark = (ImageView) row.findViewById(R.id.layoutDuasLinhasCheckMark);

                layoutDuasLinhasLinha1.setText(pais.getNome());

                layoutDuasLinhasCheckMark.setVisibility(View.INVISIBLE);

                if (pais.getNome().equals(pais.getNomeIngles())) {
                    layoutDuasLinhasLinha2.setVisibility(View.GONE);

                } else {
                    layoutDuasLinhasLinha2.setVisibility(View.VISIBLE);
                    layoutDuasLinhasLinha2.setText(pais.getNomeIngles());
                }

                if(paisSelecionado != null) {
                    if(paisSelecionado.getObjectId().equals(pais.getObjectId())) {
                        layoutDuasLinhasCheckMark.setVisibility(View.VISIBLE);
                    } else {
                        layoutDuasLinhasCheckMark.setVisibility(View.INVISIBLE);
                    }
                }
            }

            return row;
        }

        @Override
        public Filter getFilter() {

            final Filter paisesFilter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence charSequence) {

                    FilterResults resultadoPaisesFiltrado = new FilterResults();

                    if(charSequence != null && charSequence.length() > 0) {
                        ArrayList<PaisCodigo> paisesTemp = new ArrayList<PaisCodigo>();

                        for(PaisCodigo pais : paises) {
                            if(pais.getNome().toLowerCase().contains(charSequence.toString().toLowerCase()) || pais.getNomeIngles().toLowerCase().contains(charSequence.toString().toLowerCase())) {
                                paisesTemp.add(pais);

                                System.out.println(pais.getNome());
                            }
                        }

                        resultadoPaisesFiltrado.values = paisesTemp;
                        resultadoPaisesFiltrado.count = paisesTemp.size();
                    } else {
                        resultadoPaisesFiltrado.values = paises;
                        resultadoPaisesFiltrado.count = paises.size();
                    }

                    return resultadoPaisesFiltrado;
                }

                @Override
                protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

                    System.out.println(filterResults.count);

                    if(filterResults.count > 0) {
                        paisesFiltrado = (ArrayList<PaisCodigo>) filterResults.values;

                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };

            return paisesFilter;
        }
    };

    paisesListView.setAdapter(paisesAdapter);
}

And setting the Invalid index 4, size is 4 fetch.

private void configuraControladorDeBusca() {

    controladorDeBuscaEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            paisesAdapter.getFilter().filter(charSequence);

            if(controladorDeBuscaEditText.getText().toString().isEmpty()) {
                limparBuscaButton.setVisibility(View.INVISIBLE);
            } else {
                limparBuscaButton.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}

Thank you.

    
asked by anonymous 05.08.2016 / 15:09

1 answer

1

Resolved

I reimplemented the method

@Override
public int getCount() {
    return paisesFiltrado.size();
}
    
05.08.2016 / 17:20