みんな!AutocompleteTextView をコーディングしましたが、うまく機能します。ここで、オートコンプリートのドロップ ビューでリスナーを使用する必要があります。必要に応じてカスタム ArrayAdapter を実装しましたが、リスナーを使用すると execption (IndexOutOfBoundExecption) が発生します。
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Intervento intervento = (Intervento) listView.getItemAtPosition(position);
Integer idIntervento = intervento.getIdintervento();
//autoCompleteTextView.setText(intervento);
}
});
listView には、サーバーの応答が必要な空の arrayList が含まれています。
これはarrayAdapterカスタムコードです
public class AutoCompleteCustomAdapter extends ArrayAdapter<Intervento> implements Filterable {
static class ViewHolder{
private TextView textName;
}
private ArrayList<Intervento> arrayListIntervento;
private int layout;
public ArrayList<Intervento> getArrayListIntervento() {
return arrayListIntervento;
}
public void setArrayListIntervento(ArrayList<Intervento> arrayListIntervento) {
this.arrayListIntervento = arrayListIntervento;
}
@Override
public int getCount() {
return arrayListIntervento.size();
}
@Override
public Intervento getItem(int index) {
return arrayListIntervento.get(index);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
arrayListIntervento = (ArrayList<Intervento>)results.values;
setArrayListIntervento(arrayListIntervento);
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence arg0) {
FilterResults filterResults = new FilterResults();
if(arg0 != null) {
try {
arrayListIntervento = new InterventoController().execute("http://192.168.1.162:8080/Clinigo/InterventoHasSinonimoName/{interventohassinonimo_interventoNomeIntervento}/1/interventos", arg0.toString()).get();
}
catch(Exception e) {
e.printStackTrace();
}
// Now assign the values and count to the FilterResults object
filterResults.values = arrayListIntervento;
filterResults.count = arrayListIntervento.size();
}
return filterResults;
}
};
return myFilter;
}
@Override
public View getView(int position, View contentView, ViewGroup viewGroup) {
View view = null;
ViewHolder viewHolder = null;
if(contentView==null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.suggest_list, viewGroup, false);
if (view != null) {
viewHolder = new ViewHolder();
viewHolder.textName = (TextView) view.findViewById(R.id.nomeIntervento);
view.setTag(viewHolder);
Intervento intervento = arrayListIntervento.get(position);
if(intervento!=null){
viewHolder.textName.setText(intervento.getNome());
}
}
}else {
view = contentView;
viewHolder = (ViewHolder) contentView.getTag();
}
if (viewHolder != null) {
Intervento intervento = arrayListIntervento.get(position);
if(intervento!=null){
viewHolder.textName.setText(intervento.getNome());
}
}
return view;
}
public AutoCompleteCustomAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
this.arrayListIntervento= new ArrayList<Intervento>();
this.layout=textViewResourceId;
}
}
助言がありますか?
Logcat スタック トレース:
09-20 15:14:27.885: E/AndroidRuntime(941): FATAL EXCEPTION: main
09-20 15:14:27.885: E/AndroidRuntime(941): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
09-20 15:14:27.885: E/AndroidRuntime(941): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
09-20 15:14:27.885: E/AndroidRuntime(941): at java.util.ArrayList.get(ArrayList.java:304)
09-20 15:14:27.885: E/AndroidRuntime(941): at it.stasbranger.clinigomobile.AutoCompleteCustomAdapter.getItem(AutoCompleteCustomAdapter.java:42)
09-20 15:14:27.885: E/AndroidRuntime(941): at it.stasbranger.clinigomobile.AutoCompleteCustomAdapter.getItem(AutoCompleteCustomAdapter.java:1)
09-20 15:14:27.885: E/AndroidRuntime(941): at it.stasbranger.clinigomobile.MainActivity$2.onItemClick(MainActivity.java:90)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:868)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1157)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.widget.AbsListView$1.run(AbsListView.java:3168)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.os.Handler.handleCallback(Handler.java:605)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.os.Looper.loop(Looper.java:137)
09-20 15:14:27.885: E/AndroidRuntime(941): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-20 15:14:27.885: E/AndroidRuntime(941): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 15:14:27.885: E/AndroidRuntime(941): at java.lang.reflect.Method.invoke(Method.java:511)
09-20 15:14:27.885: E/AndroidRuntime(941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-20 15:14:27.885: E/AndroidRuntime(941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-20 15:14:27.885: E/AndroidRuntime(941): at dalvik.system.NativeStart.main(Native Method)