Android 由远程API填充的AutoCompleteTextView提供异常IndexOutOfBoundsException OnItemClickListener
所以我想实现一个动态的AutoCompleteTextView,它将给出要在AutoCompleteTextView下拉列表中填充的列表 下面是我编写的文件和代码Android 由远程API填充的AutoCompleteTextView提供异常IndexOutOfBoundsException OnItemClickListener,android,web-services,api,indexoutofboundsexception,autocompletetextview,Android,Web Services,Api,Indexoutofboundsexception,Autocompletetextview,所以我想实现一个动态的AutoCompleteTextView,它将给出要在AutoCompleteTextView下拉列表中填充的列表 下面是我编写的文件和代码 addLocationEdit = (AutoCompleteTextView) footerView.findViewById(R.id.add_locations_list_edit); addLocationEdit.addTextChangedListener(new TextWatcher() {
addLocationEdit = (AutoCompleteTextView) footerView.findViewById(R.id.add_locations_list_edit);
addLocationEdit.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) {
new ForLocations().execute(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
addLocationEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(Constants.TAG, "LOCATIONS LIST SIZE: " + locationList.size());
list.add(new AddLocationsBean(locationList.get(position).getId(), locationList.get(position).getName(), locationList.get(position).getDistrict(), locationList.get(position).getState()));
adapter.notifyDataSetChanged();
addLocationEdit.setText("");
}
});
addLocationEdit=(AutoCompleteTextView)footerView.findViewById(R.id.add\u locations\u list\u edit);
addLocationEdit.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
新建ForLocations()。执行;
}
@凌驾
公共无效后文本已更改(可编辑){
}
});
addLocationEdit.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Log.e(Constants.TAG,“位置列表大小:”+locationList.SIZE());
添加(新的AddLocationsBean(locationList.get(position).getId(),locationList.get(position).getName(),locationList.get(position).getDistrict(),locationList.get(position).getState());
adapter.notifyDataSetChanged();
addLocationEdit.setText(“”);
}
});
下面是我使用的异步任务:
class ForLocations extends AsyncTask<CharSequence, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
locationList = new ArrayList<>();
}
@Override
protected Void doInBackground(CharSequence... params) {
try {
CharSequence param = params[0];
URL url = new URL(Api.LOCATION_URL + "q=" + param);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
inputStream = conn.getErrorStream();
if (inputStream == null) {
inputStream = conn.getInputStream();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream), 1000);
stringBuilder = new StringBuilder();
stringBuilder.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
inputStream.close();
locationResponse = stringBuilder.toString();
} catch (Exception e) {
Log.e("LOCATION ERROR: ", "LOCATION ERROR: " + e);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
try {
// PARSE JSON
locationAdapter = new LocationAutoCompleteAdapter(MainActivity.this, locationList);
addLocationEdit.setAdapter(locationAdapter);
locationAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
class for任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
locationList=新的ArrayList();
}
@凌驾
受保护的Void doInBackground(字符序列…参数){
试一试{
CharSequence param=params[0];
URL URL=新URL(Api.LOCATION_URL+“q=“+param”);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“接受”、“应用程序/json”);
inputStream=conn.getErrorStream();
如果(inputStream==null){
inputStream=conn.getInputStream();
}
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(inputStream),1000;
stringBuilder=新的stringBuilder();
stringBuilder.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
stringBuilder.append(行+“\n”);
}
inputStream.close();
locationResponse=stringBuilder.toString();
}捕获(例外e){
Log.e(“位置错误:”,“位置错误:”+e);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
试一试{
//解析JSON
locationAdapter=新LocationAutoCompleteAdapter(MainActivity.this,locationList);
addLocationEdit.setAdapter(locationAdapter);
locationAdapter.notifyDataSetChanged();
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
下面是我正在使用的适配器:
public class LocationAutoCompleteAdapter extends ArrayAdapter<LocationBean> {
Context context;
LayoutInflater inflater;
ArrayList<LocationBean> suggestions, list, tempList;
InputStream inputStream;
StringBuilder stringBuilder;
String locationResponse;
Filter nameFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
String str = ((LocationBean) resultValue).getName();
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (LocationBean locationBean : tempList) {
if (locationBean.getName().toLowerCase().contains(constraint.toString().toLowerCase()) || locationBean.getState().toLowerCase().contains(constraint.toString().toLowerCase()) || locationBean.getDistrict().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(locationBean);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<LocationBean> filterList = (ArrayList<LocationBean>) results.values;
if (results != null && results.count > 0) {
clear();
for (LocationBean locationBean : filterList) {
add(locationBean);
notifyDataSetChanged();
}
}
}
};
public LocationAutoCompleteAdapter(Context context, ArrayList<LocationBean> list) {
super(context, R.layout.location_autocomplete_list_item, list);
this.context = context;
this.list = list;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tempList = new ArrayList<LocationBean>(list); // this makes the difference.
suggestions = new ArrayList<LocationBean>();
}
@Override
public LocationBean getItem(int position) {
return tempList.get(position);
}
@Override
public int getCount() {
return tempList.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.location_autocomplete_list_item, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.autcom_name);
holder.state = (TextView) convertView.findViewById(R.id.autcom_state);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (list.get(position).getState().isEmpty()) {
holder.state.setVisibility(View.GONE);
} else {
holder.state.setVisibility(View.VISIBLE);
}
holder.name.setText(list.get(position).getName());
holder.state.setText(list.get(position).getState());
return convertView;
}
@Override
public Filter getFilter() {
return nameFilter;
}
class ViewHolder {
TextView name, state;
}}
公共类位置自动完成适配器扩展ArrayAdapter{
语境;
充气机;
ArrayList建议、列表、圣殿骑士;
输入流输入流;
StringBuilder StringBuilder;
字符串定位响应;
过滤器名称过滤器=新过滤器(){
@凌驾
public CharSequence ConvertResultString(对象结果值){
字符串str=((LocationBean)resultValue).getName();
返回str;
}
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
if(约束!=null){
建议。清晰();
for(LocationBean LocationBean:tempList){
如果(locationBean.getName().toLowerCase().contains(constraint.toString().toLowerCase())| | locationBean.getState().toLowerCase().contains(constraint.toString().toLowerCase())| | locationBean.getDistrict().toLowerCase().contains(constraint.toString().toLowerCase()){
添加(locationBean);
}
}
FilterResults FilterResults=新的FilterResults();
filterResults.values=建议;
filterResults.count=建议.size();
返回过滤器结果;
}否则{
返回新的FilterResults();
}
}
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
List filterList=(ArrayList)results.values;
if(results!=null&&results.count>0){
清除();
for(LocationBean LocationBean:filterList){
添加(locationBean);
notifyDataSetChanged();
}
}
}
};
public LocationAutoCompleteAdapter(上下文上下文、ArrayList列表){
super(上下文、R.layout.location\u自动完成\u列表\u项目、列表);
this.context=上下文;
this.list=列表;
this.inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);
templast=newarraylist(list);//这就不同了。
建议=新建ArrayList();
}
@凌驾
公共位置Bean getItem(内部位置){
返回圣堂武士。获取(位置);
}
@凌驾
public int getCount(){
返回templast.size();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
holder=新的ViewHolder();
convertView=充气机。充气(R.layout.location\u autocomplete\u list\u项,父项,false);
holder.name=(TextView)convertView.findViewById(R.id.autcom_name);
holder.state=(TextView)convertView.findViewById(R.id.autcom_state);
convertView.setTag(支架);
}否则{
支架=(视图支架)转换器V
LocationBean bean = (LocationBean) parent.getItemAtPosition(position);
list.add(new AddLocationsBean(bean.getId(), bean.getName(), bean.getDistrict(), bean.getState()));
adapter.notifyDataSetChanged();
addLocationEdit.setText("");