1

I'm trying to get the data from remote address, put them in to a list and then do autocomplete, but i encountered a wired problem, when i first type a character in to editText, i monitored that there's correct data in the list, but the autocomplete does not work, if i delete this and type the same character again, it'll work fine, here's my code

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;
    }
};

when first type the character, logger text2 text3 text4 both appeared in logcat, but autocomplete doesn't work, does anyone know why? Thanks!

user2810081
  • 589
  • 1
  • 8
  • 27
  • call setAdapter once, not everytime you type something, see my answer here http://stackoverflow.com/questions/19858843/how-to-dynamically-add-suggestions-to-autocompletetextview-with-preserving-chara how to do that – pskink Apr 26 '14 at 08:11
  • note that you dont need to call any async requests as runQuery is called in the background thread – pskink Apr 26 '14 at 10:50
  • @pskink this works, thanks, but in this case, how can i get the string that was selected? i used a getItemAtPosition but that only gives android.database.MatrixCursor@b2e12f88 – user2810081 Apr 26 '14 at 17:41
  • its a Cursor so use getString, getInteger, getFloat etc... you can use DatabaseUtils.dumpCurrentRow to see the row data – pskink Apr 26 '14 at 19:22

1 Answers1

1

May be you need to use the same adapter and call

notifyDataSetChanged

If you don't find the answer, I suggest you switch to implementation described here

Community
  • 1
  • 1
asaf gitai
  • 400
  • 2
  • 10
  • what is styleFetcher? I tried that one, but don't understand the styleFetcher, do i need to download some jar file for it? – user2810081 Apr 26 '14 at 07:46
  • In that example StyleFetcher is a synced call to the web. you can call it since performFiltering method is called from the adapter thread (not the UI) – asaf gitai Apr 26 '14 at 07:53
  • but if i need to get a parameter from main thread and pass it to the StyleFetcher function, for example the url, how to do that? – user2810081 Apr 26 '14 at 08:02
  • you can either * pass it in the ctor * declare it as a "final" in a scope containing an override implementation of a function calling the web * declare it as a class member in a scope containing an override implementation of a function calling the web – asaf gitai Apr 28 '14 at 06:52