【问题标题】:android autocomplete doesn't work at first timeandroid自动完成第一次不起作用
【发布时间】:2014-06-12 01:09:21
【问题描述】:

我正在尝试从远程地址获取数据,将它们放入列表中,然后进行自动完成,但是我遇到了一个有线问题,当我第一次在 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;
    }
};

当第一次输入字符时,loggertext2text3text4都出现在logcat中,但是自动完成不起作用,有人知道为什么吗?谢谢!

【问题讨论】:

  • 调用 setAdapter 一次,而不是每次键入内容时,请在此处查看我的回答 stackoverflow.com/questions/19858843/… 怎么做
  • 请注意,您不需要调用任何异步请求,因为 runQuery 是在后台线程中调用的
  • @pskink 这行得通,谢谢,但在这种情况下,我怎样才能得到被选中的字符串?我使用了一个 getItemAtPosition 但这只给出了 android.database.MatrixCursor@b2e12f88
  • 它是一个游标,所以使用 getString、getInteger、getFloat 等...您可以使用 DatabaseUtils.dumpCurrentRow 查看行数据

标签: android autocomplete


【解决方案1】:

可能你需要使用同一个适配器并调用

notifyDataSetChanged

如果你没有找到答案,我建议你切换到here描述的实现

【讨论】:

  • 什么是styleFetcher?那个我试过了,但是不明白styleFetcher,我需要下载一些jar文件吗?
  • 在该示例中,StyleFetcher 是对网络的同步调用。你可以调用它,因为 performFiltering 方法是从适配器线程(不是 UI)调用的
  • 但如果我需要从主线程获取参数并将其传递给 StyleFetcher 函数,例如 url,该怎么做?
  • 您可以 * 在 ctor 中传递它 * 在包含调用 web 的函数的覆盖实现的范围内将其声明为“final” * 在包含覆盖调用网络的函数的实现
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2012-07-10
相关资源
最近更新 更多