I'm new to the Android world, and I created a small tutorial SW based on the Google 2.1 API.
At that time, I still did not know about the main threads and worker threads, so I put all my code in the main thread.
Since I fixed it with asynchronous classes for my network access to match 4.0 Google APIs.
Okay, but the last one bothers me, and I just can't find any clues.
This is an AutoCompleteTextView in the ville field ("city" in French).
BEFORE (2.1):
public void onTextChanged(CharSequence s, int start, int before, int count) { String result = null; InputStream is = null; List<String> r = new ArrayList<String>(); 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<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<String>(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<?> parent, View v, int pos, long row) { villePosition = pos; } public void onNothingSelected(AdapterView<?> arg0) { } }
works 100% perfectly:
-> after the 4th character, the query is executed on MySql to find all cities starting with 4 given letters and displays a selection list to select the correct one: OK
-> the listener gives the index of the selected city: OK
AFTER (4.0)
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<?> parent, View v, int pos, long row) { villePosition = pos; Log.d("*************9999999", "1111111111"); } public void onNothingSelected(AdapterView<?> arg0) { } } class RemplirVille extends AsyncTask<String, String, List<String>> { Integer errorMsgId; String errorMsgParam; protected List<String> doInBackground(String... param) { List<String> listeAffichageVille = new ArrayList<String>(); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 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() < 400) { HttpEntity entity = response.getEntity(); String entityStr = EntityUtils.toString(entity); JSONArray json_array = new JSONArray(entityStr); for(int i=0;i<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<String> list) { } protected void onPostExecute(List<String> 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<String>(TestActivity.this,android.R.layout.simple_selectable_list_item,list)); } } }
works with problems:
-> you need to add (enough ToFilter + 1) caractรจres to view the list of cities: BAD
-> listener not running yet: BAD
Actually enough ToFilter works well, it runs the RemplirVille class, which works fine, except that it doesn't display a list!
But, if you add another character: -> enoughToFilter still works well -> RemplirVille returns the data again ... but this time the selection list is well displayed.
Any ideas on this topic? I assume this is a context issue, but even with GetApplicationCOntext I just can't get it.
Thanks.