2

I need to fetch data from server and populate in autocompleteTextView in android. I have used a custom filter for the same where I fetch in onPerformFiltering() and display in onPublishResults(). But I need not fetch data from the server for every character Change.I need to do it for specific length of characters. How can I do this?

O__O
  • 950
  • 1
  • 9
  • 17
  • 1
    You can make use of JSON, HttpRequest. Check my answer http://stackoverflow.com/questions/8653260/autocomplete-in-android-not-working-with-dynamic-data/9451022#9451022 In this example i have used Wikipedia Suggest API. – Raghav Jun 22 '12 at 16:47
  • Thank you so much that works like a charm:) Can i use any other api instead of wikipedia suggest api in the same way. – O__O Jun 23 '12 at 03:05
  • Yup you can use any other API, you need to configure little bit to work :) since JSON structure would be changed from one API to another. – Raghav Jun 23 '12 at 05:16
  • I some times get this error and the application force closes: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. – O__O Jun 23 '12 at 15:46
  • Well android want views to be be edited by UI thread only, background threads cannot update views directly, you need to use runOnUiThread() for this. – Raghav Jun 23 '12 at 15:49
  • I have done it this way only. Even then it crashes sometimes..:( – O__O Jun 23 '12 at 15:56
  • @androiddeveloper [Here](http://stackoverflow.com/questions/5023645/how-do-i-use-autocompletetextview-and-populate-it-with-data-from-a-web-api) is an answer for a very similar question. I recommend using the getFilter function, with that you save the time of handling asynctasks, that runs the query on a background thread and then updates the list on the UI thread. – Aldo Reyes Jul 29 '12 at 18:41

1 Answers1

0
class ProcessUpdater extends AsyncTask <Void, Void, Void>{

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub

                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpget = new HttpGet("url");
                    HttpResponse response = httpclient.execute(httpget);
                    BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    StringBuffer sb = new StringBuffer("");
                    String line = "";
                    String NL = System.getProperty("line.separator");
                    while ((line = in.readLine()) != null) {
                        sb.append(line + NL);
                    }
                    in.close();
                    String result = sb.toString();
                    Log.v("My Response :: ", result);
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray resultArray = jsonObject.getJSONArray("Process");
                    for (int i = 0; i < resultArray.length(); i++) {
                        JSONObject c = resultArray.getJSONObject(i);
                        String sbs = c.getString("ProcessCode");
                        ProcessList.add(sbs);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                SelectProcess.setAdapter(new ArrayAdapter<String>(youractivtyname.this, android.R.layout.simple_dropdown_item_1line, ProcessList));

            }
        }
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59