我正在使用从服务器数据库加载的AutoCompleteTextView
。它正在填充数据,但是当我选择项目时它会抛出java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0
。我正在分享我的一些代码。
MainActivity.java
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.at_search);
autoCompleteTextView.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) {
adapter = new AutoSearchAdapter(MainActivity.this, R.layout.search_row);//custom adapter class
adapter.notifyDataSetChanged();
autoCompleteTextView.setDropDownHeight(450);
autoCompleteTextView.setAdapter(adapter);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
try {
User item = (User)adapterView.getAdapter().getItem(position);
if(item != null) {
Log.d("Adapter","Selected item:"+item.getUsername());
}
}catch (NullPointerException e){
e.printStackTrace();
}
}
});
适配器
public class AutoSearchAdapter extends ArrayAdapter<User> implements Filterable {
int layoutResourceId;
private ArrayList<User> mData;
private URLConnection ucon;
private Context context;
public AutoSearchAdapter(Context context, int textViewResourceId){
super(context, textViewResourceId);
// this.layoutResourceId = textViewResourceId;
this.context = context;
mData = new ArrayList<User>();
}
@Override
public int getCount() {
return mData.size();
}
@Nullable
@Override
public User getItem(int position) {
return mData.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.search_row, null);
}
User o = mData.get(position);
if (o != null) {
final TextView catID = (TextView) v.findViewById(R.id.tv_hello);
// TextView reviewCount = (TextView) v.findViewById(R.id.reviewCount);
final String text = o.getUsername();
catID.setText(o.getUsername());
// reviewCount.setText(o.getReviewCount());
}
return v;
}
@NonNull
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null){
try {
// A method that queries a web API, parses the data and returns an ArrayList<User>
mData = autoComplete(constraint.toString());
}catch (IOException e){
Log.e("myException", e.getMessage());
e.printStackTrace();
}
// Now assign the values and count to the FilterResults object
filterResults.values = mData;
filterResults.count = mData.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}
模型
public class User {
public String user_name = "";
public void setUsername(String user_name) {
this.user_name = user_name;
}
public String getUsername() {
return user_name;
}
}
错误:
FATAL EXCEPTION: main Process: com.example.ashfaque.autocompletetext, PID: 30429 java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at adapter.AutoSearchAdapter.getItem(AutoSearchAdapter.java:61) at com.example.ashfaque.autocompletetext.MainActivity$2.onItemClick(MainActivity.java:62) at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:906) at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1202) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1145) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3081) at android.widget.AbsListView$3.run(AbsListView.java:3947) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
任何帮助都将不胜感激。