3

I have an AutoCompleteTextView that I filter every time a new letter is typed. The filtering process contains a backround process (the runQuery method) since I am receiving data from an external database which takes a little time. This means after a new letter is typed - or deleted - the lists (array_name, array_id) are refreshed with the new data and that's what I show in the drop-down menu.

What happens by default? The filter runs, the runQuery queries the data I need, the lists are correctly filled and when I click on one of the suggestions it works just fine.

However, when I click on a suggestion the AutoCompleteTextView makes my code in my FilterProvider run AGAIN because it extends the text into the suggestion and it is considered a text change. This means that the runQuery will run again and delete the lists (array_name, array_id) instantly just before the click is executed. This only performed when I click on the suggestion before the second runQuery finishes. See:

Is it possible to somehow disable this functionality of the AutoCompleteTextView so that it doesn't extend the text when I click on the suggestion or do you have any other suggestions to solve this?

       actv_name = (AutoCompleteTextView) findViewById(R.id.actv_name);
                actv_name.setThreshold(3);
                String[] from = { "name","bid" };
                int[] to = { android.R.id.text1 };
                a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
                a.setStringConversionColumn(1);

                FilterQueryProvider provider = new FilterQueryProvider() {

                   @Override
                    public Cursor runQuery(final CharSequence constraint) {

                    Log.d("TAG", "runQuery constraint: " + constraint);
                    if (constraint == null) {
                        return null;
                    }

                    array_name.clear();
                    array_id.clear();

                    try {

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = null;
                        httppost = new HttpPost(search_goal);

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                        nameValuePairs.add(new BasicNameValuePair("newtext", constraint.toString()));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();
                        }catch(Exception e){
                                Log.e("error", "Error in http connection "+e.toString());
                    }
                        //convert response to string
                        try{
                                BufferedReader reader = new BufferedReader( new InputStreamReader(is,"iso-8859-1"),8);
                                StringBuilder sb = new StringBuilder();
                                String line = null;
                                while ((line = reader.readLine()) != null) {
                                        sb.append(line + "\n");
                                }
                                is.close();
                                //Log.i("sb", sb + "");
                                Bresult=sb.toString();
                                Log.i("Bresult", Bresult + "");
                        }catch(Exception e){
                                Log.e("error", "Error converting result "+e.toString());
                        }


                        c = new MatrixCursor(columnNames);

                        try {
                            if (Bresult != null) {
                                jArray = new JSONArray(Bresult);
                                for(int i=0;i<jArray.length();i++){
                                    JSONArray innerJsonArray = jArray.getJSONArray(i);
                                    for(int j=0;j<innerJsonArray.length();j++){
                                        JSONObject jsonObject = innerJsonArray.getJSONObject(j);
                                        array_name.add(jsonObject.getString("NAME"));
                                        array_id.add(jsonObject.getString("ID"));
                                        c.newRow().add(j).add(jsonObject.getString("NAME")).add(jsonObject.getString("ID"));
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    for (int j=0; j<array_id.size(); j++) {
                        Log.d("ACTV LOG", array_id.get(j) + ", " + array_name.get(j));

                    }
                    Log.i("ASYNC C size", c.getCount() + "");

                    return c;
                }
            };

            a.setFilterQueryProvider(provider);
            actv_name.setAdapter(a);

OnClickListener:

            actv_name.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

                    Intent intent = new Intent(NewActivity.this, ProfileActivity.class);
                    intent.putExtra("id", array_id.get(position));
                    startActivity(intent);
                    actv_name.setText("");
                }
            });
Jani Bela
  • 1,660
  • 4
  • 27
  • 50
  • i don't see any AsyncTask here... what AsyncTask are you talking about? – pskink Feb 07 '15 at 14:44
  • yes, it is just a background thread which takes time (runQuery). I fix the original question with it. Thanks for pointing it out. Any suggestion? – Jani Bela Feb 07 '15 at 14:55
  • see my answer here: http://stackoverflow.com/a/19860624/2252830 i added tv.setOnItemClickListener(l); and i don't see behavior aye are observing – pskink Feb 07 '15 at 15:20
  • wait a minute... why do you call actv_name.setText(""); ? – pskink Feb 07 '15 at 15:22
  • I call it because whenever the user clicks on a suggestion it goes to another activity and if the user presses the BACK button, i want the AutoCompleteTextView to be empty again. – Jani Bela Feb 07 '15 at 15:26
  • so set it blank in onStart/onResume, but before setting FilterQueryProvider – pskink Feb 07 '15 at 15:28
  • I put it there but obviously this is after the click is executed. What has happened now: i wrote "tes", the runQuery started and after 1 second I got the dropdown list, then I wrote "test", and immediately clicked on the suggestion (based on the search "tes") and since the lists are already cleared (the "test" runQuery started), I got a force close. So I guess if I could dismiss the dropdown every time the runQuery starts and put it back ONLY if it has finished the background work, it would be working. – Jani Bela Feb 07 '15 at 15:45
  • Is it possible to know when the runQuery finishes? – Jani Bela Feb 07 '15 at 15:48
  • i don't know, if there is no such way to find it out you most likely don't need it – pskink Feb 07 '15 at 15:54
  • Okay, I guess then i will just ignore the click if the background work hasn't finished yet. – Jani Bela Feb 07 '15 at 16:35

0 Answers0