Bonsoir;
le but de cet application est de permettre à l'utilisateur (client android)de saisir un CIN, puis récupérer la liste des exploitations enregistrées dans une BD via un web service en PHP( voir pièces jointes)
sachant que le résultat doit etre affiché dans une listView, et exactement dans cette ligne je ne comprends pas pourquoi ni le add ni le addAll ne marche!!!et aussi je n'arrive pas à repérer la listView par son id dans cette ligne:
Code : Sélectionner tout - Visualiser dans une fenêtre à part exploitations.addAll(new Exploitation(json_data.getString("CIN"), json_data.getString("exploit")));voici l'activité principale:
Code : Sélectionner tout - Visualiser dans une fenêtre à part ListView list = (ListView) getApplicationContext().findViewById(R.id.list);
l'adapteur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 package com.example.rech_exploi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.net.ParseException; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends ListActivity{ Button btnRechercher; TextView txtMsg; EditText edtMsg; JSONObject json_data=null; HttpResponse response=null; //ArrayList<String> donnees = new ArrayList<String>(); ArrayList<Exploitation> exploitations = new ArrayList<Exploitation>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnRechercher = (Button)findViewById(R.id.btnRechercher); edtMsg = (EditText) findViewById(R.id.edtMsg); } public void Rechercher(View v){ new asynchtask().execute(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class asynchtask extends AsyncTask<Void, Void, Void>{ @Override protected Void doInBackground(Void... arg0) { StringBuffer sb = new StringBuffer(""); BufferedReader br = null; try{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2/WebService/Recherche.php"); String CIN = edtMsg.getText().toString(); if(CIN.length() > 0){ try{ List<NameValuePair> donnees = new ArrayList<NameValuePair>(1); donnees.add(new BasicNameValuePair("message", CIN)); post.setEntity(new UrlEncodedFormEntity(donnees)); response = client.execute(post); //edtMsg.setText(""); //Toast.makeText(MainActivity.this, "CIN envoyé!", Toast.LENGTH_SHORT).show(); Log.e("", "CIN envoye"); } catch(ClientProtocolException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } } else Toast.makeText(MainActivity.this,"Ce champ ne peut etre vide!", Toast.LENGTH_SHORT).show(); InputStream is = response.getEntity().getContent(); br = new BufferedReader(new InputStreamReader(is)); String ligneLue = br.readLine(); while(ligneLue !=null){ sb.append(ligneLue); sb.append("\n"); ligneLue = br.readLine(); } Log.e("result", sb+""); //Toast.makeText(MainActivity.this, sb+" result", Toast.LENGTH_SHORT).show(); Log.e("", ""+sb); } catch(Exception e){ e.printStackTrace(); //Log.e("exception", e.getMessage()); Toast.makeText(MainActivity.this, "une erreur est survenue"+e.getMessage(), Toast.LENGTH_SHORT).show(); } finally{ if(br !=null){ try{ br.close(); }catch(IOException e){ e.printStackTrace(); Log.e("exception", e.getMessage()); Toast.makeText(MainActivity.this, "une erreur grave est survenue", Toast.LENGTH_SHORT).show(); } } } try{ JSONArray jArray = new JSONArray(sb.toString()); for(int i=0;i<jArray.length();i++) { json_data = jArray.getJSONObject(i); //donnees.add(json_data.getString("CIN")); //donnees.add(json_data.getString("exploit")); exploitations.addAll(new Exploitation(json_data.getString("CIN"), json_data.getString("exploit"))); } } catch(JSONException e){ Log.i("tagjsonexp",""+e.toString()); } catch (ParseException e) { Log.i("tagjsonpars",""+e.toString()); } return null; } @Override protected void onPostExecute(Void result) { ListView list = (ListView) getApplicationContext().findViewById(R.id.list); MonAdapteur adpt = new MonAdapteur(getApplicationContext(),exploitations); list.setAdapter(adpt); //setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, donnees)); edtMsg.setText(""); super.onPostExecute(result); Log.e("asynchtask", "fin"); } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package com.example.rech_exploi; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MonAdapteur extends BaseAdapter { private ArrayList<Exploitation> exploitations; private LayoutInflater myInflater; public MonAdapteur (Context context, ArrayList<Exploitation> _exploitations) { this.myInflater = LayoutInflater.from(context); this.exploitations = _exploitations; } @Override public int getCount() { return this.exploitations.size(); } @Override public Object getItem(int arg0) { return this.exploitations.get(arg0); } @Override public long getItemId(int position) { return position; } public static class ViewHolder { TextView text01; TextView text02; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = myInflater.inflate(R.layout.listitem, null); holder = new ViewHolder(); holder.text01 = (TextView) convertView.findViewById(R.id.txtNom); holder.text02 = (TextView) convertView.findViewById(R.id.txtDetail); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text01.setText(exploitations.get(position).nom); holder.text02.setText(exploitations.get(position).detail); return convertView; } }les 2 fichiers xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 package com.example.rech_exploi; public class Exploitation { String nom; String detail; public Exploitation (String _nom, String _detail) { nom = _nom; detail = _detail; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@android:id/list" android:layout_marginLeft="53dp" android:layout_toRightOf="@+id/btnRechercher" android:text="Cancel" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="14dp" android:text="Veuillez saisir votre CIN:" /> <EditText android:id="@+id/edtMsg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="19dp" android:ems="10" android:hint="@string/txt_hint_message" > <requestFocus /> </EditText> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/btnRechercher" android:layout_marginTop="22dp" > </ListView> <Button android:id="@+id/btnRechercher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/edtMsg" android:layout_marginLeft="30dp" android:onClick="Rechercher" android:text="@string/lbl_bouton" /> </RelativeLayout>s'il ya des erreurs veuillez me les signaler svp!! et Merci d'avance!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/txtNom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/txtDetail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
Partager