我的应用中有一个AutompleteTextView
。如果我输入任何字符,它的工作完全正常。 Lon for London并且它完美地显示了建议,但是如果我键入一些像12这样的数字,它根本没有显示任何建议,但如果我使用backpress删除一个字符,那么它开始显示建议。我不明白为什么当我打字并在背面显示时它没有显示。
这是我的代码
atv_search= (AutoCompleteTextView) findViewById(R.id.atv_search);
atv_search.setThreshold(1);
atv_search.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) {
PlacesTask placesTask = new PlacesTask();
placesTask.execute(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
AsyncTask和Adapter
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder sb = new StringBuilder();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class PlacesTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... place) {
// For storing data from web service
String data = "";
// Obtain browser key from https://code.google.com/apis/console
String key = "key=AIzaSyCgMSD9_CGP2-7lR7O8q-AsPTpUdxV_Z3M";
String input="";
try {
input = "input=" + URLEncoder.encode(place[0], "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// place type to be searched
String types = "types=geocode";
// Sensor enabled
String sensor = "sensor=true";
String country="&components=country:uk";
// Building the parameters to the web service
String parameters = input+"&"+types+"&"+sensor+"&"+key+"&"+country;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
try{
// Fetching the data from we service
data = downloadUrl(url);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Creating ParserTask
ParserTask parserTask = new ParserTask();
// Starting Parsing the JSON string returned by Web Service
parserTask.execute(result);
}
}
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
@Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
PlaceJsonParser placeJsonParser = new PlaceJsonParser();
try{
jObject = new JSONObject(jsonData[0]);
// Getting the parsed data as a List construct
places = placeJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
String[] from = new String[] { "description"};
int[] to = new int[] { android.R.id.text1 };
// Creating a SimpleAdapter for the AutoCompleteTextView
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
// Setting the adapter
atv_search.setAdapter(adapter);
}
}
自动完成文本视图
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/** Returns the place description corresponding to the selected item */
@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
/** Each item in the autocompetetextview suggestion list is a hashmap object */
HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
return hm.get("description");
}
}
XML
<com.phpexpert.click2eat.utils.CustomAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Enter Postal Code"
android:paddingLeft="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg"
android:id="@+id/atv_search"
android:singleLine="true"
android:ems="10"
/>
答案 0 :(得分:2)
在setAdapter();
之后使用方法 autocompletetextview.showDropDown();
它会起作用。
答案 1 :(得分:0)
因为默认序列以数字开头不是文本,所以过滤器不显示下拉列表。当您按退格键时过滤器开始检查(退格不是数字!)
所以,您可以将PHAsset
添加到atv_search.showDropDown()
以显示所有时间用户输入他/她的文字。
onTextChanged