我有一個AutoCompleteTextView
動態更新用戶輸入建議列表。我的問題是,當我輸入列表得到更新,但下拉不顯示!但是當我刪除一個字符(退格)時,下拉菜單顯示出來!Android - AutoCompleteTextView只適用於退格
我甚至試圖在文本更改後明確地調用autoText.showDropDown();
,但它不起作用。
這裏是我的TextChangedListener
:
private TextWatcher textChecker = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if(autoText.length()>9){
new GeoTask().execute();
}
}
@Override
public void afterTextChanged(Editable editable) {
if(autoText.length()>9){
autoText.showDropDown();
Log.i("UPDATE","showDropDown");
}
}
};
我甚至嘗試重置上述autoText.showDropDown();
,但仍然沒有適配器:
autoText.setAdapter(null);
autoText.setAdapter(adapter);
有沒有辦法得到它的工作?
編輯:這裏是活動的完整代碼。也許有人能找到什麼,我做錯了......
public class TestDoctor extends Activity {
TextView latText;
TextView lngText;
AutoCompleteTextView autoText;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_doctor);
latText = (TextView) findViewById(R.id.latTextView);
lngText = (TextView) findViewById(R.id.longTextView);
autoText = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button = (Button) findViewById(R.id.buttonDoctor);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new GeoTask().execute();
}
});
autoText.setThreshold(10);
autoText.setHint("Οδός Αριθμός, Περιοχή");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(true);
autoText.addTextChangedListener(textChecker);
}
private TextWatcher textChecker = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3){}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if(autoText.length()>9){
new GeoTask().execute();
}
}
@Override
public void afterTextChanged(Editable editable) {}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private List<SimpleAddress> getAddressesFromText(String address) {
address = address.replace(' ', '+');
HttpHelper httpHelper = new HttpHelper();
InputStream inputStream = httpHelper.makeHttpGetRequest("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
String response = httpHelper.inputStreamToString(inputStream);
List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();
try {
JSONObject jsonObject = new JSONObject(response);
int size = ((JSONArray) jsonObject.get("results")).length();
for (int i = 0; i<size; i++){
String formatted_address = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getString("formatted_address");
Double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
Double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
simpleAddresses.add(i,new SimpleAddress(formatted_address,lat,lng));
}
return simpleAddresses;
} catch (JSONException e) {
return null;
}
}
public class GeoTask extends AsyncTask<Void, Void, Void> {
List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();
@Override
protected Void doInBackground(Void... voids) {
Log.i("UPDATE","1");
try {
simpleAddresses = getAddressesFromText(autoText.getText().toString());
} catch (NullPointerException e) {
}
Log.i("UPDATE","2");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.i("UPDATE", "3");
int size = simpleAddresses.size();
if(size > 0){
adapter.clear();
Log.i("ADAPTER_SIZE",""+size);
for (int i = 0; i< size; i++){
adapter.add(simpleAddresses.get(i).getFormatted_address());
Log.i("ADDED",simpleAddresses.get(i).getFormatted_address());
}
Log.i("UPDATE","4");
autoText.setAdapter(null);
autoText.setAdapter(adapter);
autoText.showDropDown();
Log.i("UPDATE","showDropDown");
}
super.onPostExecute(aVoid);
}
}
}
EDIT 2 我也試過,但它仍然無法正常工作。下拉只能說明當我按退格...
@Override
public void afterTextChanged(Editable editable) {
runOnUiThread(updateAdapter);
}
...
private Runnable updateAdapter = new Runnable() {
public void run(){
Log.i("ADAPTER_UPDATE","start");
adapter.notifyDataSetChanged();
autoText.showDropDown();
Log.i("ADAPTER_UPDATE","end");
}
};
注意 的問題是:我鍵入一個地址。當我輸入什麼都沒有顯示出來。但我在logcat中看到適配器已更新。但下拉式不顯示。然後,當我刪除一個字符的下拉菜單顯示??!
你可以嘗試adapter.setNotifyOnChange(假的),然後手動調用adapter.notifyDataSetChanged()在onPostExecute()的結尾。 –
我剛試過。它不工作... –