当用户在AutoCompleteTextView中键入内容时,我在异步任务中从数据库中获取数据。我在AutoCompleteTextView中使用TextWatcher添加了addTextChangedListener。问题是什么时候 用户从建议列表中选择一个项目,该数据被输入文本观察者,但下拉列表甚至对用户可见。因为,当用户从下拉列表中选择一个项目时,会再次调用TextWatcher.onTextChanged()并且此调用会发送一个新请求。这是第一次选择项目时发生的情况,如果用户再次单击该项目,则下拉列表将不会显示。
那么当用户第一次从建议列表中选择项目时,如何隐藏此下拉列表?
我做到了:
autoComplete = (AutoCompleteTextView) findViewById(R.id.my_text);
final TextWatcher yourTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged:" + s.toString());
afterTextChanged = s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged:" + s.toString());
beforeTextChanged = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, "beforeTextChanged:" + beforeTextChanged + ",afterTextChanged:" + afterTextChanged + ",onTextChanged:" + s.toString());
if (!beforeTextChanged.equals(s.toString())) {
new doPopulate().execute(s.toString());
}
}
};
Logcat:在输入“go”后选择某个项目(Google)
beforeTextChanged:go
beforeTextChanged:go,afterTextChanged:go,onTextChanged:Google
afterTextChanged:Google
答案 0 :(得分:0)
如果用户选择,则保留一个布尔标志,并将onItemClickListener添加到AutoCompleteTextView,即
boolean selectedText = false;
autoComplete.setOnItemClickedListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
selectedText = true;
}
});
然后检查TextWatcher中的selectedText
标志:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, "beforeTextChanged:" + beforeTextChanged + ",afterTextChanged:" + afterTextChanged + ",onTextChanged:" + s.toString());
if (!beforeTextChanged.equals(s.toString()) && !selectedText) { //Here we check selectedText
new doPopulate().execute(s.toString());
}
selectedText = false; //Clear selectedText flag
}
干杯
答案 1 :(得分:0)
在 onTextChanged 方法中添加以下代码行:
if(autoComplete.isPerformingCompletion()) {
// An item has been selected from the list.
return;
}
答案 2 :(得分:0)
我在自动完成下拉列表中显示的ArrayAdapter项目之前添加了双空格,当用户从下拉列表中选择项目时,当onTextChanged触发时检查条件字符串不等于double空格然后执行asyn任务。
ontextchanged code
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String SearchProd = s.toString();
if (SearchProd.length()>=2){
String SearchProdSpace=SearchProd.substring(0,2);
if(SearchProdSpace!=" " && itemselected==false)
{
try{
new GetProductData(SearchProd).execute();
}catch(Exception e){ e.printStackTrace();}
}
}
itemselected=false;
}
AsyncTask代码
public class GetProductData extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(context);
GetProductData(String str) {
try {
qryText= URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
protected void onPreExecute() {
Dialog.setMessage("Loading ...");
Dialog.setCanceledOnTouchOutside(false);
Dialog.show();
}
@Override
protected Void doInBackground(String... params) {
try {
JSONObject json = new JSONObject(getProductData());
ProductList=new ArrayList<String>();
Status = json.getBoolean("Status");
if (Status) {
JSONArray Products=json.getJSONArray("Products");
if (Products.length() > 0) {
for (int i = 0; i < Products.length(); i++) {
JSONObject ProductObj = Products.getJSONObject(i);
ProductList.add(" "+ProductObj.getString("Name"));
}
}
} else {
Error = json.getString("Message");
}
} catch (JSONException e) {
Error = e.getMessage();
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
if (Error != null) {
Toast.makeText(context, "Some Error Occured : " + Error,
Toast.LENGTH_LONG).show();
} else {
try {
ArrayAdapter<String> Arrayadapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line,ProductList);
Arrayadapter.setNotifyOnChange(true);
autoComplete.setAdapter(Arrayadapter);
autoComplete.showDropDown();
autoComplete.requestFocus();
autoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
itemselected=true;
String itemstr=ProductList.get(position).trim();
autoComplete.setText(itemstr);
}
});
// ItemName.setSelected(selected)
} catch (Exception e) {e.printStackTrace(); }
}
}
}