gpt4 book ai didi

java - 将 AutoCompleteTextView 预测仅限于餐馆

转载 作者:行者123 更新时间:2023-12-01 11:35:57 29 4
gpt4 key购买 nike

我有一个 Android 应用程序,允许用户根据餐馆名称执行详细信息搜索。但是,根据用户输入,预测可能包含地点、国家/地区等。我在哪里可以添加限制以仅检查餐厅名称?

我当前的代码:

public static ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;

HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE
+ TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
System.out.println("URL: " + url);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());

// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
resultList = new ArrayList<String>(predsJsonArray.length());
place = new HashMap<String, String>();
for (int i = 0; i < predsJsonArray.length(); i++) {
// System.out.println(predsJsonArray.getJSONObject(i).getString(
// "description"));
// System.out
// .println("============================================================");
resultList.add(predsJsonArray.getJSONObject(i).getString(
"description"));

String description = predsJsonArray.getJSONObject(i).getString("description");
String placeId = predsJsonArray.getJSONObject(i).getString("place_id");
place.put( description, placeId);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}

最佳答案

根据place types您能得到的最接近的是development 过滤器。为了使用restaurant过滤器,您必须执行搜索而不是自动完成。

使用适用于 Android 的 Places API PlaceComplete我尝试将 Place.TYPE_ESTABLISHMENTPlace.TYPE_RESTAURANT 传递给 AutocompleteFilter.create 进行验证。

关于java - 将 AutoCompleteTextView 预测仅限于餐馆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991138/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com