我试图在我的应用程序的自动建议中捕获事件侦听器。我正在使用 wiki-suggest 填充列表:http ://en.wikipedia.org/w/api.php?action=opensearch&search=%22bike%22&limit=8&namespace=0&format=json 。
我把所有的事情都做好了,但现在我只想单击列表中的项目并将字符串带到下一个活动。我已经通过: http: //developer.android.com/reference/android/widget/AutoCompleteTextView.html#setOnItemClickListener (android.widget.AdapterView.OnItemClickListener )
但这并没有让我知道如何实现它。
我在此处粘贴我的代码以进行自动建议填充。请建议如何克服这个问题。
@Override
protected String doInBackground(String... key) {
String newText = key[0];
newText = newText.trim();
newText = newText.replace(" ", "+");
try {
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet(
"http://en.wikipedia.org/w/api.php?action=opensearch&search="
+ newText + "&limit=8&namespace=0&format=json");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
suggest = new ArrayList<String>();
JSONArray jArray = new JSONArray(data);
for (int i = 0; i < jArray.getJSONArray(1).length(); i++) {
String SuggestKey = jArray.getJSONArray(1).getString(i);
suggest.add(SuggestKey);
}
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
runOnUiThread(new Runnable() {
public void run() {
aAdapter = new ArrayAdapter<String>(
getApplicationContext(), R.layout.item, suggest);
autoComplete.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
这就是我从 wiki-suggest 获得新建议的方式:
autoComplete = (AutoCompleteTextView) findViewById(R.id.text);
autoComplete.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString();
new getJson().execute(newText);
}
});