| 12
 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
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 
 | public class  MajContactActivity  extends Activity {
 
    EditText txtNom;
    EditText txtPrenom;
    EditText txtNummobile;
    EditText txtNumfixe;
    EditText txtEmail;
    EditText txtAdresse;
    EditText txtProfession;
 
    Button btnSav;
    Button btnSup;
 
    String idcontact;
 
    // Progress Dialog
    private ProgressDialog pDialog;
 
    // JSON parser class
    JSONParser jsonParser = new JSONParser();
 
    // single contact url
    private static final String url_detail_contact = "http://10.0.2.2/etc/etc.php";
 
    // url to update contact
    private static final String url_update_contact = "http://10.0.2.2/etc/etc.php";
 
    // url to delete contact
    private static final String url_delete_contact = "http://10.0.2.2/etc/etc.php";
 
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_CONTACT = "contact";
    private static final String TAG_IDCONTACT = "idcontact";
 
	private static final String TAG_NOM = "nom";
	private static final String TAG_PRENOM = "prenom";
	private static final String TAG_NUMMOBILE = "nummobile";
	private static final String TAG_NUMFIXE = "numfixe";
	private static final String TAG_EMAIL = "email";
	private static final String TAG_ADRESSE = "adresse";
	private static final String TAG_PROFESSION = "profession";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maj_contact);
 
        // save button
        btnSav = (Button) findViewById(R.id.btnSav);
        btnSup = (Button) findViewById(R.id.btnSup);
 
        // getting contact details from intent
        Intent i = getIntent();
 
        // getting contact id (idcontact) from intent
        idcontact = i.getStringExtra(TAG_IDCONTACT);
 
        // Getting complete contact details in background thread
        new Get_Detail_Contact().execute();
 
        // save button click event
        btnSav.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // starting background task to update contact
                new Sav_Detail_Contact().execute();
            }
        });
 
        // Delete button click event
        btnSup.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // deleting contact in background thread
                new Sup_Contact().execute();
            }
        });
 
    }
 
    /**
     * Background Async Task to Get complete contact details
     * */
    class Get_Detail_Contact extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MajContactActivity.this);
            pDialog.setMessage("Chargement du contact. Veuillez patientez...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Getting contact details in background thread
         * */
        protected String doInBackground(String... params) {
 
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("idcontact", idcontact));
 
                        // getting contact details by making HTTP request
                        // Note that contact details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_detail_contact, "GET", params);
 
                        // check your log for json response
                        Log.d("Detail contact unique", json.toString());
 
                        // json success tag
                        success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            // successfully received contact details
                            JSONArray contactObj = json
                                    .getJSONArray(TAG_CONTACT); // JSON Array
 
                            // get first contact object from JSON Array
                            JSONObject contact = contactObj.getJSONObject(0);
 
                            // contact with this idcontact found
                            // Edit Text
							txtNom = (EditText) findViewById(R.id.inputNom);
							txtPrenom = (EditText) findViewById(R.id.inputPrenom);
							txtNummobile = (EditText) findViewById(R.id.inputNumMobile);
							txtNumfixe = (EditText) findViewById(R.id.inputNumFixe);
							txtEmail = (EditText) findViewById(R.id.inputEmail);
							txtAdresse = (EditText) findViewById(R.id.inputAdresse);
							txtProfession = (EditText) findViewById(R.id.inputProfession);
 
                            // display contact data in EditText
							txtNom.setText(contact.getString(TAG_NOM));
							txtPrenom.setText(contact.getString(TAG_PRENOM));
							txtNummobile.setText(contact.getString(TAG_NUMMOBILE));
							txtNumfixe.setText(contact.getString(TAG_NUMFIXE));
							txtEmail.setText(contact.getString(TAG_EMAIL));
							txtAdresse.setText(contact.getString(TAG_ADRESSE));
							txtProfession.setText(contact.getString(TAG_PROFESSION));
 
                        }else{
                            // contact with idcontact not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }
 
    /**
     * Background Async Task to  Save contact Details
     * */
    class Sav_Detail_Contact extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MajContactActivity.this);
            pDialog.setMessage("Sauvegarde du contact ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Saving contact
         * */
        protected String doInBackground(String... args) {
 
            // getting updated data from EditTexts
			String nom = txtNom.getText().toString();
			String prenom = txtPrenom.getText().toString();
			String nummobile = txtNummobile.getText().toString();
			String numfixe = txtNumfixe.getText().toString();
			String email = txtEmail.getText().toString();
			String adresse = txtAdresse.getText().toString();
			String profession = txtProfession.getText().toString();
 
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
 
            params.add(new BasicNameValuePair(TAG_IDCONTACT, idcontact));
			params.add(new BasicNameValuePair(TAG_NOM, nom));
			params.add(new BasicNameValuePair(TAG_PRENOM, prenom));
			params.add(new BasicNameValuePair(TAG_NUMMOBILE, nummobile));
			params.add(new BasicNameValuePair(TAG_NUMFIXE, numfixe));
			params.add(new BasicNameValuePair(TAG_EMAIL, email));
			params.add(new BasicNameValuePair(TAG_ADRESSE, adresse));
			params.add(new BasicNameValuePair(TAG_PROFESSION, profession));
 
            // sending modified data through http request
            // Notice that update contact url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_update_contact,
                    "POST", params);
 
            // check json success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
 
                if (success == 1) {
                    // successfully updated
                    Intent i = getIntent();
                    // send result code 100 to notify about contact update
                    setResult(100, i);
                    finish();
                } else {
                    // failed to update contact
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once contact uupdated
            pDialog.dismiss();
        }
    }
 
    /*****************************************************************
     * Background Async Task to Delete Product
     * */
    class Sup_Contact extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MajContactActivity.this);
            pDialog.setMessage("Suppression du contact...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Deleting contact
         * */
        protected String doInBackground(String... args) {
 
            // Check for success tag
            int success;
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("idcontact", idcontact));
 
                // getting contact details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        url_delete_contact, "POST", params);
 
                // check your log for json response
                Log.d("Suppression du contact", json.toString());
 
                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // contact successfully deleted
                    // notify previous activity by sending code 100
                    Intent i = getIntent();
                    // send result code 100 to notify about contact deletion
                    setResult(100, i);
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once contact deleted
            pDialog.dismiss();
 
        }
 
    }
} | 
Partager