0

我试图在我的应用程序的自动建议中捕获事件侦听器。我正在使用 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); 
    } 
}); 
4

2 回答 2

1

从评论更新
你初始化的地方autoComplete,可能在onCreate(),像这样添加你的 OnItemClickListener:

autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String text = ((TextView) view).getText().toString();
        new getJson().execute(text);
    }
});
于 2012-09-07T17:27:06.313 回答
0

解决了。只需将整个字符串传递给 ItemClickListener 上的 URL。

于 2012-09-07T19:36:48.747 回答