在Android中使用异步任务自定义自动完成
我正在使用AsyncTask实现自定义自动完成。AsyncTask用于从服务器获取所需数据。 我这样做如下在Android中使用异步任务自定义自动完成,android,android-asynctask,autocomplete,Android,Android Asynctask,Autocomplete,我正在使用AsyncTask实现自定义自动完成。AsyncTask用于从服务器获取所需数据。 我这样做如下 AutoCompleteTextView request_recommendation_fragment_employee_name; ArrayList<AutoCompleteItems> ArrayEmployeeName = new ArrayList<AutoCompleteItems>(); MyAutoCompleteAdapter Adapter_
AutoCompleteTextView request_recommendation_fragment_employee_name;
ArrayList<AutoCompleteItems> ArrayEmployeeName = new ArrayList<AutoCompleteItems>();
MyAutoCompleteAdapter Adapter_Employee_Name;
AsyncTaskAdvanceSearchRecommendationFragment advanceSearchRecommendationFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layoutView = inflater.inflate(R.layout.fragment_request_recommendation, container, false);
request_recommendation_fragment_employee_name = (AutoCompleteTextView) layoutView.findViewById(R.id
.request_recommendation_fragment_employee_name);
request_recommendation_fragment_employee_name.setThreshold(1);
Adapter_Employee_Name = new MyAutoCompleteAdapter(getActivity(), ArrayEmployeeName);
request_recommendation_fragment_employee_name.setAdapter(Adapter_Employee_Name);
request_recommendation_fragment_employee_name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// ApplicationStaticMethods.toastDebug(getActivity(),"On text change");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// ApplicationStaticMethods.toastDebug(getActivity(),"Before text change");
}
@Override
public void afterTextChanged(Editable s) {
//
advanceSearchRecommendationFragment = new AsyncTaskAdvanceSearchRecommendationFragment();
advanceSearchRecommendationFragment.execute();
// ApplicationStaticMethods.toastDebug(getActivity(),"After text change");
}
});
return layoutView;
}
public class MyAutoCompleteAdapter extends ArrayAdapter<AutoCompleteItems> implements Filterable {
private Activity context;
private ArrayList<AutoCompleteItems> values;
public MyAutoCompleteAdapter(Activity context, ArrayList<AutoCompleteItems> items) {
super(context, 0, items);
this.values = items;
this.context = context;
}
public int getCount() {
return values.size();
}
// public Object getItem(int position) {
// return values.get(position);
// }
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.spinner_item_reg,
parent, false);
}
AutoCompleteItems spTemp = values.get(position);
TextView autocompleteItem = (TextView) convertView
.findViewById(R.id.regSpinnerTextview);
convertView.setTag(spTemp.getId());
autocompleteItem.setText(spTemp.getName().toString());
autocompleteItem.setBackgroundColor(Color.CYAN);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public Filter getFilter() {
return null;
}
}
public class AutoCompleteItems {
private String _id;
private String _name;
public AutoCompleteItems(String _id, String _name) {
this._id = _id;
this._name = _name;
}
public String getId() {
return this._id;
}
public void setId(String id) {
this._id = id;
}
public String getName() {
return this._name;
}
public void setName(String name) {
this._name = name;
}
}
private class AsyncTaskAdvanceSearchRecommendationFragment extends AsyncTask<String, Void, Void> {
//All operations of getting data
//Data object received in variable responseString
..............................
.............................
..............................
protected void onPostExecute(Void result) {
if (progress.isShowing()) {
progress.dismiss();
}
if (!responseString.equals("")) {
AutoCompleteItems nameObj;
JSONArray JsonArray;
try {
JSONObject Jsonobject1 = new JSONObject(responseString);
String status_message = Jsonobject1.getString("status_message");
if (status_message.equals("Passed")) {
String data = Jsonobject1.getString("data");
// ApplicationStaticMethods.toastDebug(getActivity(),data);
JsonArray = new JSONArray(data);
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject Jsonobject = JsonArray.getJSONObject(i);
if (webFlag == "empname") {
String name = Jsonobject.getString("EMP_NAME");
nameObj = new AutoCompleteItems("", name);
ArrayEmployeeName.add(nameObj);
}
}
Adapter_Employee_Name.notifyDataSetChanged();
}
} catch (JSONException e) {
Log.e("Error", "Error: " + e.toString());
}
} else {
}
}
requestRecommensions.java文件的内容如下
AutoCompleteTextView request_recommendation_fragment_employee_name;
ArrayList<AutoCompleteItems> ArrayEmployeeName = new ArrayList<AutoCompleteItems>();
MyAutoCompleteAdapter Adapter_Employee_Name;
AsyncTaskAdvanceSearchRecommendationFragment advanceSearchRecommendationFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layoutView = inflater.inflate(R.layout.fragment_request_recommendation, container, false);
request_recommendation_fragment_employee_name = (AutoCompleteTextView) layoutView.findViewById(R.id
.request_recommendation_fragment_employee_name);
request_recommendation_fragment_employee_name.setThreshold(1);
Adapter_Employee_Name = new MyAutoCompleteAdapter(getActivity(), ArrayEmployeeName);
request_recommendation_fragment_employee_name.setAdapter(Adapter_Employee_Name);
request_recommendation_fragment_employee_name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// ApplicationStaticMethods.toastDebug(getActivity(),"On text change");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// ApplicationStaticMethods.toastDebug(getActivity(),"Before text change");
}
@Override
public void afterTextChanged(Editable s) {
//
advanceSearchRecommendationFragment = new AsyncTaskAdvanceSearchRecommendationFragment();
advanceSearchRecommendationFragment.execute();
// ApplicationStaticMethods.toastDebug(getActivity(),"After text change");
}
});
return layoutView;
}
public class MyAutoCompleteAdapter extends ArrayAdapter<AutoCompleteItems> implements Filterable {
private Activity context;
private ArrayList<AutoCompleteItems> values;
public MyAutoCompleteAdapter(Activity context, ArrayList<AutoCompleteItems> items) {
super(context, 0, items);
this.values = items;
this.context = context;
}
public int getCount() {
return values.size();
}
// public Object getItem(int position) {
// return values.get(position);
// }
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.spinner_item_reg,
parent, false);
}
AutoCompleteItems spTemp = values.get(position);
TextView autocompleteItem = (TextView) convertView
.findViewById(R.id.regSpinnerTextview);
convertView.setTag(spTemp.getId());
autocompleteItem.setText(spTemp.getName().toString());
autocompleteItem.setBackgroundColor(Color.CYAN);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public Filter getFilter() {
return null;
}
}
public class AutoCompleteItems {
private String _id;
private String _name;
public AutoCompleteItems(String _id, String _name) {
this._id = _id;
this._name = _name;
}
public String getId() {
return this._id;
}
public void setId(String id) {
this._id = id;
}
public String getName() {
return this._name;
}
public void setName(String name) {
this._name = name;
}
}
private class AsyncTaskAdvanceSearchRecommendationFragment extends AsyncTask<String, Void, Void> {
//All operations of getting data
//Data object received in variable responseString
..............................
.............................
..............................
protected void onPostExecute(Void result) {
if (progress.isShowing()) {
progress.dismiss();
}
if (!responseString.equals("")) {
AutoCompleteItems nameObj;
JSONArray JsonArray;
try {
JSONObject Jsonobject1 = new JSONObject(responseString);
String status_message = Jsonobject1.getString("status_message");
if (status_message.equals("Passed")) {
String data = Jsonobject1.getString("data");
// ApplicationStaticMethods.toastDebug(getActivity(),data);
JsonArray = new JSONArray(data);
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject Jsonobject = JsonArray.getJSONObject(i);
if (webFlag == "empname") {
String name = Jsonobject.getString("EMP_NAME");
nameObj = new AutoCompleteItems("", name);
ArrayEmployeeName.add(nameObj);
}
}
Adapter_Employee_Name.notifyDataSetChanged();
}
} catch (JSONException e) {
Log.e("Error", "Error: " + e.toString());
}
} else {
}
}
我的任务如下
AutoCompleteTextView request_recommendation_fragment_employee_name;
ArrayList<AutoCompleteItems> ArrayEmployeeName = new ArrayList<AutoCompleteItems>();
MyAutoCompleteAdapter Adapter_Employee_Name;
AsyncTaskAdvanceSearchRecommendationFragment advanceSearchRecommendationFragment;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layoutView = inflater.inflate(R.layout.fragment_request_recommendation, container, false);
request_recommendation_fragment_employee_name = (AutoCompleteTextView) layoutView.findViewById(R.id
.request_recommendation_fragment_employee_name);
request_recommendation_fragment_employee_name.setThreshold(1);
Adapter_Employee_Name = new MyAutoCompleteAdapter(getActivity(), ArrayEmployeeName);
request_recommendation_fragment_employee_name.setAdapter(Adapter_Employee_Name);
request_recommendation_fragment_employee_name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// ApplicationStaticMethods.toastDebug(getActivity(),"On text change");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// ApplicationStaticMethods.toastDebug(getActivity(),"Before text change");
}
@Override
public void afterTextChanged(Editable s) {
//
advanceSearchRecommendationFragment = new AsyncTaskAdvanceSearchRecommendationFragment();
advanceSearchRecommendationFragment.execute();
// ApplicationStaticMethods.toastDebug(getActivity(),"After text change");
}
});
return layoutView;
}
public class MyAutoCompleteAdapter extends ArrayAdapter<AutoCompleteItems> implements Filterable {
private Activity context;
private ArrayList<AutoCompleteItems> values;
public MyAutoCompleteAdapter(Activity context, ArrayList<AutoCompleteItems> items) {
super(context, 0, items);
this.values = items;
this.context = context;
}
public int getCount() {
return values.size();
}
// public Object getItem(int position) {
// return values.get(position);
// }
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context
.getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.spinner_item_reg,
parent, false);
}
AutoCompleteItems spTemp = values.get(position);
TextView autocompleteItem = (TextView) convertView
.findViewById(R.id.regSpinnerTextview);
convertView.setTag(spTemp.getId());
autocompleteItem.setText(spTemp.getName().toString());
autocompleteItem.setBackgroundColor(Color.CYAN);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
@Override
public Filter getFilter() {
return null;
}
}
public class AutoCompleteItems {
private String _id;
private String _name;
public AutoCompleteItems(String _id, String _name) {
this._id = _id;
this._name = _name;
}
public String getId() {
return this._id;
}
public void setId(String id) {
this._id = id;
}
public String getName() {
return this._name;
}
public void setName(String name) {
this._name = name;
}
}
private class AsyncTaskAdvanceSearchRecommendationFragment extends AsyncTask<String, Void, Void> {
//All operations of getting data
//Data object received in variable responseString
..............................
.............................
..............................
protected void onPostExecute(Void result) {
if (progress.isShowing()) {
progress.dismiss();
}
if (!responseString.equals("")) {
AutoCompleteItems nameObj;
JSONArray JsonArray;
try {
JSONObject Jsonobject1 = new JSONObject(responseString);
String status_message = Jsonobject1.getString("status_message");
if (status_message.equals("Passed")) {
String data = Jsonobject1.getString("data");
// ApplicationStaticMethods.toastDebug(getActivity(),data);
JsonArray = new JSONArray(data);
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject Jsonobject = JsonArray.getJSONObject(i);
if (webFlag == "empname") {
String name = Jsonobject.getString("EMP_NAME");
nameObj = new AutoCompleteItems("", name);
ArrayEmployeeName.add(nameObj);
}
}
Adapter_Employee_Name.notifyDataSetChanged();
}
} catch (JSONException e) {
Log.e("Error", "Error: " + e.toString());
}
} else {
}
}
我的问题是一切似乎都很好。当我使用调试模式查看变量时,每个变量都按要求设置,并且我从AsyncTask中获得了正确的数据。但是当我在AutoComplete中输入一些值时,它并没有给出任何建议。
请告诉我哪里出了问题,如果可能,请向我提供已更正的代码删除自动完成文本视图的文本监视程序,您应该在getFilter方法中调用异步任务为什么禁用了筛选?