Comment récupérer le texte sélectionné dans un ListView avec onListItemClick
Tout marche bien jusqu'a ce que je clique sur un élément de la listview. autrement dire quand je solllicite "onListItemClick". Merci d'avance
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| public class Listedesservices extends ListActivity{
ArrayList<Listedepartement> Listedepartement= new ArrayList<Listedepartement>();
private static String url ="http://10.0.2.2/masante/liste_service.php";
String result = null;
InputStream is = null;
JSONObject json_data=null;
ProgressDialog progressDialog;
DBCesarienne db;
ListView list;
int i1=0;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_cesarienne);
new MyAsyncTask().execute();
progressDialog = ProgressDialog.show(this, "", "Loading...please wait", true);
progressDialog.setCancelable(true);
}
/**
* Cette classe montre une simple dérivation de la classe AsyncTask
*/
class MyAsyncTask extends AsyncTask<Void, Integer, String> {
// Surcharge de la méthode doInBackground (Celle qui s'exécute dans une Thread à part)
@Override
protected String doInBackground(Void... unused) {
try{
//commandes httpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
Log.i("Le retour1", ""+httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.i("Le retour2", ""+is);
// récupérer la liste
//conversion de la réponse en chaine de caractère
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
JSONArray jArray = new JSONArray(result);
db=new DBCesarienne(getBaseContext());
db.open();
db.Truncate();
Log.i("Le retour3", "Ouverture et vidange de la base");
// parcourir toute la liste
for(int i = 0; i < jArray.length(); i++){
// récupérer un employé de type JSONObject
json_data = jArray.getJSONObject(i);
String n1=""+json_data.getString("Libelleservice");
Log.i("Le retour4", ""+n1);
db.Inserercesarienne(""+n1);
}
db.close();
}
catch (/*JSON*/Exception e)
{
e.printStackTrace();
}
return ("J'ai fini de parcourrir le net");
}
// Surcharge de la méthode onPostExecute (s'exécute dans la Thread de l'IHM)
@Override
protected void onPostExecute(String message) {
db=new DBCesarienne(Listedesservices.this);
final ListView list=(ListView) Listedesservices.this.findViewById(android.R.id.list);
TextView info=(TextView)findViewById(R.id.info);
info.setText("Choisissez le profil du spécialiste");
db.open();
Cursor c=db.recuperercesarienne();
while(c.moveToNext())
{
Listedepartement.add(new Listedepartement(c.getString(1)));
AdaptateurCesarienne adp =new AdaptateurCesarienne(Listedesservices.this,Listedepartement);
list.setAdapter(adp);
//System.out.println(Listedepartement.add(new Listedepartement(c.getString(1))));
}
c.close();
db.close();
progressDialog.dismiss();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
ListView list=(ListView) Listedesservices.this.findViewById(android.R.id.list);
String s=(String) list.getItemAtPosition(position);
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
} |