简体   繁体   中英

Android / MySQL: Delete data at MySQL database from ListView

Currently, I created an app that contains list view. I uses the library "com.baoyz.swipemenulistview:library:1.3.0" to display my list of data.

In this list view, I add a function to delete the data. If the user want to delete one item from the list view, just swipe from right to left, and a button 'delete' will display.

Now, I already code the button delete, so that the item that choose by the user will delete. The problem is after the user click the button delete, it will toast "Item Deleted." But if the user opens back the list view, it doesn't delete. I don't know why. below is my code:

JAVA

    listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
            switch (index) {
                case 0:
                    String report_id = null;

                    deleteTask(report_id);

                    Toast.makeText(TaskList.this,"Item deleted",Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(TaskList.this, Home.class);
                    startActivity(intent);
                    break;
            }
            // false : close the menu; true : not close the menu
            return false;
        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TaskClass taskClass = taskClassArrayList.get(position);

            Intent intent = new Intent(TaskList.this, TaskUpdateBefore.class);
            intent.putExtra("task", taskClass);
            startActivity(intent);
        }

    });

    retrieveJSON();

}


private void deleteTask(final String report_id) {

    showSimpleProgressDialog(this, "Loading...","Please wait",true);
    StringRequest stringRequest=new StringRequest(Request.Method.POST, URLs.URL_DELETE_TASK, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Toast.makeText(TaskList.this,response, Toast.LENGTH_LONG).show();

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String, String> getParams()  {
            Map<String,String>parms=new HashMap<String, String>();
            parms.put("report_id",report_id);
            return parms;
        }
    };
    RequestQueue requestQueue= Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(stringRequest);

}

delete_task.php

<?php
require_once "../config/configPDO.php";

$report_id = $_POST['report_id'];


$sql = "DELETE ot_report WHERE report_id = '$report_id'";
$query = $conn->prepare($sql);
$query->execute();

if($query){
    echo "Data Save!";
}else{
    echo "Error!! Not Saved";
}

?>  

Can anyone help me. Pleaseee

Forgot "FROM" keyword this is a syntax for sql query delete

DELETE FROM ot_report WHERE report_id = '$report_id';

Android

TaskClass taskObject = (TaskClass) listView.getItemAtPosition(position); 
reportId = taskObject.getId()//your field;
deleteTask(reportId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM