Bonjour à tous,
Je réalise une application dans laquelle je souhaite afficher et mettre à jour des taux stockés dans une base de données mySQL.
J'ai suivi un tuto qui me permets un affichage sous forme de gridview. Ca fonctionne bien mais malheureusement, cela ne correspond à mes attentes. Ce que je veux, c'est afficher mes taux dans un GridLayout préconçu. Par exemple, dans mon premier textview du GridLayout, je veux que soit afficher le taux correspondant au PID = "1", et ainsi de suite pour les autres textview. Aussi, je ne souhaite pas d'affichage sous forme de liste. Et là, je bloque...
Ma table mySQL possède les colonnes suivantes :
PID - nom - nvtaux - date
Voici mon code pour un affichage en gridview
fragment.xml
list.xml
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 <android.support.v7.widget.CardView 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" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_marginBottom="8dp" android:layout_marginTop="65dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" app:cardCornerRadius="3dp" app:cardElevation="5dp" app:cardPreventCornerOverlap="false" app:contentPadding="0dp" tools:context="Fragment"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:alignmentMode="alignBounds" android:layout_marginTop="40dp" android:columnCount="3" android:padding="10dp" android:layout_marginBottom="40dp"> </GridView> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:paddingBottom="10dp" android:text="Update"/> </RelativeLayout></ScrollView></android.support.v7.widget.CardView>
Fragment.java
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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="8dp" android:orientation="vertical" > <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:alignmentMode="alignBounds" android:layout_marginTop="40dp" android:columnCount="3" android:padding="10dp" android:layout_marginBottom="40dp"> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/pid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"/> <TextView android:id="@+id/maladie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:textSize="14.0sp" android:textStyle="bold" android:text="Maladie"/> <TextView android:id="@+id/psmaladie" //doit afficher la donnée "nvtaux" pour le PID = "1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/ppmaladie" //doit afficher la donnée "nvtaux" pour le PID = "2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/vieillesse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_columnSpan="1" android:layout_gravity="left|center_vertical" android:paddingRight="30dp" android:textSize="14.0sp" android:textStyle="bold" android:text="Vieillesse"/> <TextView android:id="@+id/psvieillesse" //doit afficher la donnée "nvtaux" pour le PID = "3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/ppvieillesse" //doit afficher la donnée "nvtaux" pour le PID = "4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </GridLayout>
et mon fichier PHP
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.GridView; import android.widget.ListAdapter; import android.widget.SimpleAdapter; import android.widget.TextView; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Fragment extends Fragment { // Progress Dialog private ProgressDialog pDialog; View rootView; GridView grid; // Creating JSON Parser object JSONParser jParser = new JSONParser(); ArrayList<HashMap<String, String>> tauxxList; // JSON Node names private static final String TAG_SUCCESS = "success"; private static final String TAG_PRODUCTS = "tauxx"; private static final String TAG_PID = "pid"; private static final String TAG_NVTAUX = "nvtaux"; // products JSONArray JSONArray tauxx = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); rootView = inflater.inflate(R.layout.fragment, container, false); // Inflate the layout for this fragment // Hashmap for ListView tauxxList = new ArrayList<HashMap<String, String>>(); grid = (GridView) rootView.findViewById(R.id.grid); // Loading products in Background Thread new LoadAllProducts().execute(); // on seleting single product // launching Edit Product Screen grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // getting values from selected ListItem String pid = ((TextView) rootView.findViewById(R.id.pid)).getText() .toString(); // Starting new intent Intent in = new Intent(getActivity().getApplicationContext(), EditData.class); // sending pid to next activity in.putExtra(TAG_PID, pid); // starting new activity and expecting some response back startActivityForResult(in, 100); } }); return rootView; } // Response from Edit Product Activity @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // if result code 100 if (resultCode == 100) { // if result code 100 is received // means user edited/deleted product // reload this screen again Intent intent = getActivity().getIntent(); getActivity().finish(); startActivity(intent); } } /** * Background Async Task to Load all product by making HTTP Request * */ class LoadAllProducts extends AsyncTask<String, String, String> { /** * Before starting background thread Show Progress Dialog * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(getActivity()); pDialog.setMessage("Loading products. Please wait..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } /** * getting All products from url * */ protected String doInBackground(String... args) { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(getResources().getString(R.string.all_products), "GET", params); // Check your log cat for JSON reponse Log.d("All Products: ", json.toString()); try { // Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); if (success == 1) { // products found // Getting Array of Products tauxx = json.getJSONArray(TAG_PRODUCTS); // looping through All Products for (int i = 0; i < tauxx.length(); i++) { JSONObject c = tauxx.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_PID); String nvtaux = c.getString(TAG_NVTAUX); // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put(TAG_PID, id); map.put(TAG_NVTAUX, nvtaux); // adding HashList to ArrayList tauxxList.add(map); } } else { // no products found // Launch Add New product Activity } } catch (JSONException e) { e.printStackTrace(); } return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog after getting all products pDialog.dismiss(); // updating UI from Background Thread getActivity().runOnUiThread(new Runnable() { public void run() { /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter( getActivity(), tauxxList, R.layout.list, new String[]{TAG_NVTAUX}, new int[]{R.id.psmaladie}); // updating listview grid.setAdapter(adapter); } }); } } }
Pouvez-vous alors m'aiguiller parce que je n'arrive pas à comprendre comment je peux récupérer la valeur précise d'un champ de ma base, et ainsi l'afficher dans un textview de mon GridLayout ?
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 <?php /* * Following code will list all the products */ // array for JSON response $response = array(); // include db connect class require_once 'db_connect.php'; // connecting to db $db = new DB_CONNECT(); // get all products from products table $result = mysql_query("SELECT *FROM tauxx") or die(mysql_error()); // check for empty result if (mysql_num_rows($result) > 0) { // looping through all results // products node $response["tauxx"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $taux = array(); $taux["pid"] = $row["pid"]; $taux["nom"] = $row["nom"]; $taux["nvtaux"] = $row["nvtaux"]; $taux["date"] = $row["date"]; $taux["created_at"] = $row["created_at"]; // push single product into final response array array_push($response["tauxx"], $taux); } // success $response["success"] = 1; // echoing JSON response echo json_encode($response); } else { // no products found $response["success"] = 0; $response["message"] = "No products found"; // echo no users JSON echo json_encode($response); } ?>
En vous remerciant par avance pour votre aide !
Partager