Android - Custom Arrayadapter problems with content

Asked

Viewed 54 times

0

I’m trying to make a ListView with a ArrayAdapter customized. When I use the ArrayList original all functional well but when I update the ArrayList and use notifyDataSetChanged() to update the values of ListView the app stops working and returns the error: Invalid index 4, size is 4

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 the configuration of EditText search.

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.

  • 2

    No methods are required: public int getCount() { Return list.size(); } public Object getItem(int i) { Return list.get(i); } public long getItemId(int i) { Return i; } ?

  • It worked! Thank you.

1 answer

1


Solved

I reimplemented the method

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

Browser other questions tagged

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