Error IndexOutOfBoundsException when selecting item in an AutoCompleteTextView

2

I have a AutoCompleteTextView that shows the matches from a query SQL . Matches are in a List<Predio> where Predio is an object with the data of a terrain. The AutoCompleteTextView only works well when I select the first item and when selecting from the second I get the following error:

Exception

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.prueba.sti.adapters.PredioAdapter.getItem(PredioAdapter.java:42)
at com.prueba.sti.adapters.PredioAdapter.getItem(PredioAdapter.java:21)
at android.widget.AdapterView.getItemAtPosition(AdapterView.java:764)
at com.prueba.sti.InventarioFragment$2.onItemClick(InventarioFragment.java:75)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:902)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1192)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
at android.widget.AbsListView$3.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)

Adapter

public class PredioAdapter extends ArrayAdapter<Predio> implements Filterable {

    private LayoutInflater mInflater = null;
    Context context;
    List<Predio> predios = null;


    public PredioAdapter(Context context, List<Predio> predios) {
        super(context, 0, predios);
        this.context = context;
        this.predios = predios;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Predio getItem(int position) {
        return this.predios.get(position);
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        Predio predio = getItem(position);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.dropdown_item, parent, false);
        }

        TextView txtSISCOP = (TextView) convertView.findViewById(R.id.txtCodigoSISCOPItem);
        TextView txtNombre = (TextView) convertView.findViewById(R.id.txtNombrePredio);

        txtSISCOP.setText(predio.getCodigo_siscop());
        txtNombre.setText(predio.getNombre());

        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    Log.i("ADAPTER", predios.toString());
                    Log.i("ADAPTER", predios.size() + "");
                    filterResults.values = predios;
                    filterResults.count = predios.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;

    }

}

And this is the onCreateView() from where the Adapter is called:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_inventario, container, false);

    try {
        acPredios = (AutoCompleteTextView) view.findViewById(R.id.acPredios);
        acPredios.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                predios = new PredioSQL(getContext()).getPredios(s.toString());
                adapter = new PredioAdapter(getContext(), predios);
                acPredios.setAdapter(adapter);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        acPredios.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Predio predio = (Predio) parent.getItemAtPosition(position);
                Log.i("onItemSelected", predio.getId_remota() + "");
                Log.i("onItemSelected", predio.getCodigo());
                Log.i("onItemSelected", parent.getItemAtPosition(position).toString());
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
    return view;
}
    
asked by Marcelo 12.02.2016 в 03:17
source

1 answer

1

Your problem is that you must obtain the object Predio from its position (index) in the list of objects Predio and in Adapter , the index (position) will always be the same and avoids generating the error IndexOutOfBoundsException .

 acPredios.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             // incorrecto!  Predio predio = (Predio) parent.getItemAtPosition(position);
                Log.i("onItemSelected", String.valueOf(predios.get(position).getId_remota()));
                Log.i("onItemSelected", predios.get(position).getCodigo());
                Log.i("onItemSelected", String.valueOf(position));
            }
        });
    
answered by 12.02.2016 в 04:20