Tengo un AutoCompleteTextView
que muestra las coincidencias desde una consulta SQL
. Las coincidencias están en una List<Predio>
donde Predio es un objeto con los datos de un terreno. El AutoCompleteTextView
solo funciona bien cuando selecciono el primer item y al seleccionar desde el segundo obtengo el siguiente 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;
}
}
Y este es el onCreateView()
desde donde se llama el Adapter
:
@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;
}