可能重复:
带有 AutocompleteTextView 和 onclicklistener 的 ArrayAdapter 自定义
我正在努力理解我的 Android 应用程序中发生的事情,该应用程序使用了一个很好的自定义自动完成类。特别是我提出休息请求,获取数据,填充 ArrayList(我需要更多信息),使用过滤器并显示建议列表(根据服务器响应是正确的)。但是,当我单击项目时,我的应用程序崩溃了。建议??
这是我的适配器类:
package xxx;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
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;
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/.../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;
}
}
在我的活动中,我调用:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
Intervento intervento = ((Intervento) listView.getAdapter().getItem(position));
Integer idIntervento = intervento.getIdintervento();
//autoCompleteTextView.setText(intervento);
}
});
这是日志猫:
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)
建议?