android autocomplete第一次不起作用

时间:2014-04-26 07:27:49

标签: android autocomplete

我正在尝试从远程地址获取数据,将它们放入列表然后执行自动完成,但我遇到了有线问题,当我第一次输入一个字符到editText时,我监控到了正确的数据列表,但自动完成不起作用,如果我删除它并再次键入相同的字符,它会工作正常,这是我的代码

private OnKeyListener mKeyListener = new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
      // do something when the button is clicked
        if (event.getAction() == KeyEvent.ACTION_UP) {
            //do something here

            companyList.clear();
            EditText getInput = (EditText) findViewById(R.id.editText1);

            char pressedKey = (char) event.getUnicodeChar();
            String inputStr = Character.toString(pressedKey);
            inputStr = getInput.getText().toString();
            Logger.getLogger("test1").info(inputStr);

            if(keyCode == 67){
                return false;
            }
            String urlStr = "http://autoc.finance.yahoo.com/autoc?query=" + inputStr +
                    "&callback=YAHOO.Finance.SymbolSuggest.ssCallback";

            Logger.getLogger("url success").info(urlStr);

            AsyncHttpClient client = new AsyncHttpClient();
            Logger.getLogger("client success").info(urlStr);
            client.get(urlStr, new AsyncHttpResponseHandler() {
                public void onSuccess(String response) {
                    Logger.getLogger("testsuccess").info(response);
                    String jString = (String) response.subSequence(39, response.length()-1);
                    try {
                        JSONObject jsonObj = new JSONObject(jString);
                        JSONArray jsonArr = jsonObj.getJSONObject("ResultSet").getJSONArray("Result");
                        int i=0;
                        for(i = 0; i < jsonArr.length(); i++){
                            JSONObject tmpObj = jsonArr.getJSONObject(i);
                            String line = tmpObj.getString("symbol") + ", " + tmpObj.getString("name") + " (" + tmpObj.getString("exch") + ")";
                            companyList.add(line);
                        }
                        JSONObject firstObj = jsonArr.getJSONObject(0);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    for(String word : companyList){
                        Logger.getLogger("companyList").info(word);
                    }

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                            android.R.layout.simple_list_item_1, companyList);
                    AutoCompleteTextView textView = (AutoCompleteTextView)
                            findViewById(R.id.editText1);
                    Logger.getLogger("text2").info(textView.getText().toString());
                    textView.setAdapter(adapter);
                    Logger.getLogger("text3").info(textView.getText().toString());
                    textView.setThreshold(1);
                    Logger.getLogger("text4").info(textView.getText().toString());
}); 
                }
            });

            return false;
        }
        return false;
    }
};

首次输入字符时,记录器text2 text3 text4都出现在logcat中,但自动填充不起作用,有人知道原因吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可能需要使用相同的适配器并致电

notifyDataSetChanged

如果您找不到答案,我建议您切换到here所述的实施方案