如何使用 API 调用填充 Android 中的 AutoCompleteTextView?
How to fill AutoCompleteTextView in Android with an API call?
我想尝试从 API 中获取食品列表并基于此更新 AutoCompleteTextView。
我尝试按照此处的答案进行操作,但无济于事:
到目前为止,这是我的代码:
//Outside onCreate
List<String> apiFoods = new ArrayList<>();
//In onCreate
AutoCompleteTextView autocomplete = (AutoCompleteTextView) findViewById(R.id.foodActv);
final ArrayAdapter<String> autoAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, apiFoods);
autocomplete.setAdapter(autoAdapter);
autocomplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//retrieve data s
}
@Override
public void afterTextChanged(Editable s) {
retrieveData(s);
autoAdapter.notifyDataSetChanged();
Log.d("TAG", "foodsApi is " + apiFoods);
}
});
//Below onCreate
private void retrieveData(Editable s)
{
String text = s.toString();
if(text.contains(" "))
{
text.replace(" ", "%20");
}
String url = "https://api.edamam.com/api/food-database/parser?ingr="+text+"&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f";
new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
{
try
{
JSONObject foodNames=new JSONObject(new String(responseBody));
JSONArray jArray = foodNames.getJSONArray("hints");
for(int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject hintItem = jArray.getJSONObject(i);
JSONObject foodItem = hintItem.getJSONObject("food");
String foodLabel = foodItem.getString("label");
apiFoods.add(foodLabel);
}
catch(JSONException e)
{
}
}
} catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_SHORT).show();
}
});
}
您可以在此处查看示例 API 响应:https://api.edamam.com/api/food-database/parser?ingr=red&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f
目前,此代码正在将正确的食物名称输入 apiFoods ArrayList
,但它没有向我显示 select 在应用程序中的选项。
请注意,您在进行 api 调用之前就已经初始化了数据适配器,因此我建议当您的 api returns 数据进入 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
函数时, 请按如下方式重新初始化您的适配器。
private void retrieveData(final AutoCompleteTextView s)
{
String text = s.toString();
if(text.contains(" "))
{
text.replace(" ", "%20");
}
String url = "https://api.edamam.com/api/food-database/parser?ingr="+text+"&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f";
new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
{
try
{
JSONObject foodNames=new JSONObject(new String(responseBody));
JSONArray jArray = foodNames.getJSONArray("hints");
for(int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject hintItem = jArray.getJSONObject(i);
JSONObject foodItem = hintItem.getJSONObject("food");
String foodLabel = foodItem.getString("label");
apiFoods.add(foodLabel);
}
catch(JSONException e)
{
}
}
autoAdapter = new ArrayAdapter<>(ActivityName.this, android.R.layout.simple_dropdown_item_1line, apiFoods);
s.setAdapter(autoAdapter);
s.setThreshold(1);
s.setAdapter(adapter);
} catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_SHORT).show();
}
});
}
我想尝试从 API 中获取食品列表并基于此更新 AutoCompleteTextView。
我尝试按照此处的答案进行操作,但无济于事:
到目前为止,这是我的代码:
//Outside onCreate
List<String> apiFoods = new ArrayList<>();
//In onCreate
AutoCompleteTextView autocomplete = (AutoCompleteTextView) findViewById(R.id.foodActv);
final ArrayAdapter<String> autoAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, apiFoods);
autocomplete.setAdapter(autoAdapter);
autocomplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//retrieve data s
}
@Override
public void afterTextChanged(Editable s) {
retrieveData(s);
autoAdapter.notifyDataSetChanged();
Log.d("TAG", "foodsApi is " + apiFoods);
}
});
//Below onCreate
private void retrieveData(Editable s)
{
String text = s.toString();
if(text.contains(" "))
{
text.replace(" ", "%20");
}
String url = "https://api.edamam.com/api/food-database/parser?ingr="+text+"&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f";
new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
{
try
{
JSONObject foodNames=new JSONObject(new String(responseBody));
JSONArray jArray = foodNames.getJSONArray("hints");
for(int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject hintItem = jArray.getJSONObject(i);
JSONObject foodItem = hintItem.getJSONObject("food");
String foodLabel = foodItem.getString("label");
apiFoods.add(foodLabel);
}
catch(JSONException e)
{
}
}
} catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_SHORT).show();
}
});
}
您可以在此处查看示例 API 响应:https://api.edamam.com/api/food-database/parser?ingr=red&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f
目前,此代码正在将正确的食物名称输入 apiFoods ArrayList
,但它没有向我显示 select 在应用程序中的选项。
请注意,您在进行 api 调用之前就已经初始化了数据适配器,因此我建议当您的 api returns 数据进入 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
函数时, 请按如下方式重新初始化您的适配器。
private void retrieveData(final AutoCompleteTextView s)
{
String text = s.toString();
if(text.contains(" "))
{
text.replace(" ", "%20");
}
String url = "https://api.edamam.com/api/food-database/parser?ingr="+text+"&app_id=8ff4be18&app_key=f2bf020e6d3cf1a9989c2a2163fb720f";
new AsyncHttpClient().get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
{
try
{
JSONObject foodNames=new JSONObject(new String(responseBody));
JSONArray jArray = foodNames.getJSONArray("hints");
for(int i = 0; i < jArray.length(); i++)
{
try
{
JSONObject hintItem = jArray.getJSONObject(i);
JSONObject foodItem = hintItem.getJSONObject("food");
String foodLabel = foodItem.getString("label");
apiFoods.add(foodLabel);
}
catch(JSONException e)
{
}
}
autoAdapter = new ArrayAdapter<>(ActivityName.this, android.R.layout.simple_dropdown_item_1line, apiFoods);
s.setAdapter(autoAdapter);
s.setThreshold(1);
s.setAdapter(adapter);
} catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(), "API call failed", Toast.LENGTH_SHORT).show();
}
});
}