値をデータベースに更新するとき、毎回 1 行を更新するのではなく (これが必要でした)、データベースの列全体を更新します。ここに私のコードがあります:
Java コード:
private Dialog alertDialog() { final AlertDialog.Builder
alertDialog = new AlertDialog.Builder(NotificationActivity.this);
// Setting Dialog Title alertDialog.setTitle("Confirmation..."); // Setting Dialog Message alertDialog.setMessage("Do you want to accept this job?"); // Setting Icon to Dialog alertDialog.setIcon(R.drawable.ic_dialog_alert); // Setting Positive "Yes" Button alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Accept", Toast.LENGTH_SHORT).show(); // Return Result from alertdialog to database result = "accepted"; System.out.println("The result is "+ result); new UpdateActivity().execute(); } }); // Setting Negative "NO" Button alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Write your code here to execute after dialog Toast.makeText(getApplicationContext(), "Reject", Toast.LENGTH_SHORT).show(); dialog.cancel(); // Return Result from alertdialog to database result = "rejected"; System.out.println("The result is "+ result); new UpdateActivity().execute(); } }); return alertDialog.show(); }
/** * データベースを更新するバックグラウンド非同期タスク * */ class UpdateActivity extends AsyncTask {
/** * バックグラウンド スレッドを開始する前に進行状況ダイアログを表示 * */ @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(NotificationActivity.this); pDialog.setMessage("送信中..."); pDialog.setIndeterminate(false); pDialog.setCancelable(真); pDialog.show(); }
/** * 製品を保存中 * */ protected String doInBackground(String... args) { System.out.println("Sending...");
// getting updated data from dialog String confirmation =
結果.toString(); System.out.println("結果を文字列に..." + 確認);
// Building Parameters List<NameValuePair> params = new
配列リスト(); params.add(新しい BasicNameValuePair(TAG_ID, uid)); params.add(新しい BasicNameValuePair(TAG_RESULT, 確認)); // JSON オブジェクトを取得します // create product url は POST メソッドを受け入れることに注意してください JSONObject json = jsonParser.makeHttpRequest(UPDATE_URL, "POST", params); System.out.println("Json 解析中..."); // レスポンスのログ cat をチェック Log.d("Create Response", json.toString());
// check for success tag try { System.out.println("Checking..."); int success = json.getInt(TAG_SUCCESS); if (success == 1) { // successfully created product Intent i = new Intent(getApplicationContext(), JobsAcceptedActivity.class); startActivity(i); //Toast.makeText(getApplicationContext(), "Update successfully...", Toast.LENGTH_SHORT).show(); System.out.println("Successfully updated..."); // closing this screen finish(); } else { // failed to create product //Toast.makeText(getApplicationContext(), "Update unsucessfully...", Toast.LENGTH_SHORT).show(); System.out.println("Update unsuccessfully..."); } } catch (JSONException e) { e.printStackTrace(); } System.out.println("Done!"); return null; } /** * After completing background task Dismiss the progress dialog * **/ protected void onPostExecute(String file_url) { // dismiss the dialog once done pDialog.dismiss(); } } }
PHP コード:
$response = 配列(); // 必要なフィールドを確認します if (isset($_POST['UID']) && isset($_POST['accept'])) {
$UID = $_POST['UID']; $accept = $_POST['accept']; // include db connect class require_once __DIR__ . '/db_connect.php'; // connecting to db $db = new DB_CONNECT(); // mysql update row with matched pid $result = mysql_query("UPDATE notification SET accept = '$accept' WHERE UID = $UID"); // check if column inserted or not if ($result) { // successfully updated $response["success"] = 1; $response["message"] = "Notification successfully updated."; // echoing JSON response echo json_encode($response); } else { } } else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) is missing"; // echoing JSON response echo json_encode($response);