Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoCompleteTextView & Async class
    primarykey
    data
    text
    <p>I am a rookie in the Android world and I built up a small training SW based on the 2.1 Google API.</p> <p>At that time I did not know yet about main thread and worker threads, so I put all my code in the main thread.</p> <p>Since, I fixed it with async classes for my netwkork access to fit the 4.0 Google API.</p> <p>Ok, but one last thing bothers me and I just can not find any clues.</p> <p>It is about an <em>AutoCompleteTextView</em> on a field <em>ville</em> ("town" in french).</p> <hr> <p><strong>BEFORE (2.1):</strong></p> <p></p> <pre><code>public void onTextChanged(CharSequence s, int start, int before, int count) { String result = null; InputStream is = null; List&lt;String&gt; r = new ArrayList&lt;String&gt;(); if (ville.enoughToFilter()) { is = connexionHttp(BASE_URL + "ville.php?ville=" + ville.getText()); result = lectureData(is); try { JSONArray jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i&lt;jArray.length();i++) { json_data = jArray.getJSONObject(i); r.add(json_data.getString("VILLE")); a_idVil.add(json_data.getString("CLEF_VILLE")); } ville.setAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_selectable_list_item,r)); ville.setOnItemSelectedListener(new villeListener()); } catch(JSONException e1) { Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); Log.d("***** TestActivity/onTextChanged: JSONException *****", "--"+e1.toString()+"--"); } catch(ParseException e1) { Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show(); Log.d("***** TestActivity/onTextChanged: ParseException *****", "--"+e1.toString()+"--"); } } } public class villeListener implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos, long row) { villePosition = pos; } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { } } </code></pre> <p> </p> <p>runs 100% perfect:</p> <p>-> after the 4th caracters, the query runs on MySql to find all the towns beginning with the 4 given letters, and displays the selection list to select the right one: OK</p> <p>-> the listener give the index of the choosen town: OK</p> <hr> <p><strong>AFTER (4.0)</strong></p> <p></p> <pre><code>public void onTextChanged(CharSequence s, int start, int before, int count) { if (ville.enoughToFilter()) { new RemplirVille().execute(BASE_URL + "ville.php?ville=" + ville.getText()); Log.d("***********","AVANT"); ville.setOnItemSelectedListener(new villeListener()); Log.d("***********","APRES"); } } public class villeListener implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, View v, int pos, long row) { villePosition = pos; Log.d("*************9999999", "1111111111"); } public void onNothingSelected(AdapterView&lt;?&gt; arg0) { } } class RemplirVille extends AsyncTask&lt;String, String, List&lt;String&gt;&gt; { Integer errorMsgId; String errorMsgParam; protected List&lt;String&gt; doInBackground(String... param) { List&lt;String&gt; listeAffichageVille = new ArrayList&lt;String&gt;(); ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(param[0]); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() &lt; 400) { HttpEntity entity = response.getEntity(); String entityStr = EntityUtils.toString(entity); JSONArray json_array = new JSONArray(entityStr); for(int i=0;i&lt;json_array.length();i++) { JSONObject json_ligne = json_array.getJSONObject(i); listeAffichageVille.add(json_ligne.getString("VILLE")); a_idVil.add(json_ligne.getString("CLEF_VILLE")); } } else { Log.d("***** TestActivity/ASYNC RemplirVille: EXCEPTION http error *****", "--"+response.getStatusLine().toString()+"--"); this.errorMsgId = R.string.http_site_error; listeAffichageVille = null; } } catch (Exception ex) { Log.d("***** TestActivity/ASYNC RemplirVille: EXCEPTION decode error *****", "--"+ex.toString()+"--"); this.errorMsgId = R.string.http_decode_error; this.errorMsgParam = ex.getLocalizedMessage(); listeAffichageVille = null; } return listeAffichageVille; } protected void onProgressUpdate(String... item) { } protected void onPreExecute(List&lt;String&gt; list) { } protected void onPostExecute(List&lt;String&gt; list) { if (list == null) { if (this.errorMsgId != null) { String msg = TestActivity.this.getString(this.errorMsgId); Toast.makeText(TestActivity.this,msg,Toast.LENGTH_LONG).show(); } } else { ville.setAdapter(new ArrayAdapter&lt;String&gt;(TestActivity.this,android.R.layout.simple_selectable_list_item,list)); } } } </code></pre> <p></p> <p>runs with troubles:</p> <p>-> you have to put in (enoughToFilter + 1) caractères to diplay the list of the towns: BAD</p> <p>-> the listener is even not run anymore: BAD</p> <hr> <p>In fact <strong>enoughToFilter</strong> works well, it launches the RemplirVille class which runs ok except that it does not displays the list!</p> <p>But, if you put in 1 more caracter: -> <strong>enoughToFilter</strong> still working well -> RemplirVille brings the data one more time.... but this time the selection list displays well.</p> <p>Any idea about that topic? I guess it is a context problem, but even with a GetApplicationCOntext I just can not get it.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload