Reputation: 694
I have a listView
in Activity A. When long press detected, it will check whether the iD is the maximum. If yes, list will be deleted and list will be refreshed. Otherwise it will display "list cannot be deleted".
listViewUpdate.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {
iD = details1.get(po).getID();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Delete");
builder.setMessage("Are you sure you want to delete?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
checkMaxID(po,ID,iD);
RetrieveTotalHours(ID);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int ii) {
dialog.dismiss();
}
}
);
builder.show();
return true;
}
});
return edit_details;
}
public void checkMaxID(final int po,final int foreignKey,final String iD)
{
class check extends AsyncTask<Void,Void,String>{
// ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
// loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
try{
JSONObject json = new JSONObject(s);
if(json.getBoolean("success")){
objadapter.removeItem(po);
objadapter.notifyDataSetChanged();
Toast.makeText(getActivity(), json.getString("message"), Toast.LENGTH_LONG).show();
Log.e("List", "deleted");
}else{
Toast.makeText(getActivity(), json.getString("message"), Toast.LENGTH_LONG).show();
Log.e("List", " Not deleted");
}
}catch(JSONException ex){
ex.printStackTrace();
Log.e("LISDSSS","ex="+ex.toString());
}
}
@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(Configs.KEY_ID, iD);
hashMap.put(Configs.KEY_TWD, String.valueOf(foreignKey));
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(Configs.URL_CHECK_ID, hashMap);
return s;
}
}
check ue = new check();
ue.execute();
}
php
<?php
$json = array();
if(isset($_POST['id'], $_POST['twd'])){
/*Importing our db connection script*/
require_once('dbConnect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = mysqli_real_escape_string($con, $_POST['id']);
$twd = mysqli_real_escape_string($con, $_POST['twd']);
$sql ="SELECT MAX(id) as MaxId FROM work_details WHERE twd = '$twd'";
if ($result = mysqli_query($con, $sql)) {
/* fetch associative array */
if ($row = mysqli_fetch_row($result)) {
if($row[0] === $id){
$sql ="DELETE FROM work_details WHERE id='$id';";
if ($result = mysqli_query($con, $sql)) {
echo 'success';
$json['success'] = true;
$json['message'] = 'delete is successful';
}else{
$json['success'] = false;
$json['message'] = 'list cannot be deleted';
}
}else
echo 'list cannot be deleted';
$json['success'] = false;
$json['message'] = '($row[0] !== $id)';
}
} else{
$json['success'] = false;
$json['message'] = 'select is unsuccessful';
}
/* close connection */
mysqli_close($con);
}
?>
The list that has maximum iD get deleted in MySQL
, but the problem is list not refreshing.
LogCat
E/LISDSSS﹕ ex=org.json.JSONException: Value success of type java.lang.String cannot be converted to JSONObject
This is what I get from ex.printStackTrace();
, in the catch block.
Any help would be greatly appreciated.
Upvotes: 0
Views: 537
Reputation: 6828
There are two methods to solve this. This is the method I feel better or comfortable. Just as @Nikiole91 pointed out remove the echo
from the php script. Also print the JSON output.
PHP
<?php
$json = array();
if(isset($_POST['id'], $_POST['twd'])){
/*Importing our db connection script*/
require_once('dbConnect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = mysqli_real_escape_string($con, $_POST['id']);
$twd = mysqli_real_escape_string($con, $_POST['twd']);
$sql ="SELECT MAX(id) as MaxId FROM work_details WHERE twd = '$twd'";
if ($result = mysqli_query($con, $sql)) {
/* fetch associative array */
if ($row = mysqli_fetch_row($result)) {
if($row[0] === $id){
$sql ="DELETE FROM work_details WHERE id='$id';";
if ($result = mysqli_query($con, $sql)) {
$json['success'] = true;
$json['message'] = 'delete is successful';
}else{
$json['success'] = false;
$json['message'] = 'list cannot be deleted';
}
}else
$json['success'] = false;
$json['message'] = '($row[0] !== $id)';
}
} else{
$json['success'] = false;
$json['message'] = 'select is unsuccessful';
}
/* close connection */
mysqli_close($con);
}
//print the json
echo json_encode($json);
?>
Now your android code should work fine.
Upvotes: 0
Reputation: 350
The php method is returning the String "success" because of the echo in:
$sql ="DELETE FROM work_details WHERE id='$id';";
if ($result = mysqli_query($con, $sql)) {
echo 'success';
$json['success'] = true;
$json['message'] = 'delete is successful';
The method onPostExecute() fails here
JSONObject json = new JSONObject(s);
because the result String 's' is the String "success" returned by the echo instead of the $json object.
Try removing those echos.
Upvotes: 2