我有一个AutoCompleteTextView,当我们在这个url中触摸它时,我想在其中显示来自Web服务的建议。问题是只要我触摸它就会显示建议,当我移除触摸时它会停止显示建议。即使我已经删除了触摸AutoCompleteTextView,我也希望显示该建议。所以任何建议将不胜感激。感谢。
public class MainActivity extends AppCompatActivity{
AutoCompleteTextView auto;
private List<String> suggest;
private ArrayAdapter<String> adapter;
private static final String SPANAME = "My_API_Link";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
suggest = new ArrayList<String>();
auto = (AutoCompleteTextView)findViewById(R.id.auto);
auto.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
prepareSpa();
auto.showDropDown();
auto.requestFocus();
return false;
}
});
}
private void prepareSpa(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, SPANAME,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
try {
suggest = new ArrayList<String>();
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
String dataAdd = jsonarray.getString(i);
if(!dataAdd.equals(null))
suggest.add(dataAdd);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, suggest);
auto.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("location", "Dummy Text");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
&#13;
答案 0 :(得分:0)
我尝试了自定义的AutoCompleteTextView here,并通过一些反复试验获得了预期的结果。
public class MainActivity extends AppCompatActivity{
InstantAutoComplete auto;
private List<String> suggest;
private ArrayAdapter<String> adapter;
private static final String SPANAME = "My_API_Link";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
suggest = new ArrayList<String>();
auto = (InstantAutoComplete)findViewById(R.id.auto);
auto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prepareSpa();
auto.showDropDown();
}
});
}
private void prepareSpa(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, SPANAME,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getActivity(), response, Toast.LENGTH_SHORT).show();
try {
suggest = new ArrayList<String>();
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
String dataAdd = jsonarray.getString(i);
if(!dataAdd.equals(null))
suggest.add(dataAdd);
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, suggest);
auto.setAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "VolleyError" + error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("location", "Dania,Florida,33312");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);
}
}
&#13;
public class InstantAutoComplete extends AutoCompleteTextView {
public InstantAutoComplete(Context context) {
super(context);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (getWindowVisibility() == View.GONE) {
Log.d("InstantAutoComplete", "Window not visible, will not show drop down");
return;
}
if (focused) {
//performFiltering(getText(), 0);
showDropDown();
}
}
}
&#13;