【问题标题】:Android / MySQL: Delete data at MySQL database from ListViewAndroid / MySQL:从 ListView 中删除 MySQL 数据库中的数据
【发布时间】:2020-05-01 23:50:10
【问题描述】:

目前,我创建了一个包含列表视图的应用程序。我使用库“com.baoyz.swipemenulistview:library:1.3.0”来显示我的数据列表。

在这个列表视图中,我添加了一个删除数据的函数。如果用户想从列表视图中删除一项,只需从右向左滑动,就会显示一个“删除”按钮。

现在,我已经编写了删除按钮的代码,这样用户选择的项目就会被删除。问题是用户单击按钮删除后,它会吐司“已删除项目”。但如果用户打开列表视图,它不会删除。我不知道为什么。以下是我的代码:

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";
}

?>  

谁能帮帮我。拜托了

【问题讨论】:

  • 您的代码易受 SQL 注入攻击。您应该在准备好的语句中使用参数绑定。

标签: java php android mysql listview


【解决方案1】:

忘记了“FROM”关键字 这是sql查询删除的语法

DELETE FROM ot_report WHERE report_id = '$report_id';

安卓

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

【讨论】:

  • 在您的代码中“案例 0: String report_id = null; deleteTask(report_id);”你发送空
  • 以及如何获取当前id?
  • 您为列表视图使用自定义适配器?
  • 使用这个:TaskClass taskObject = (TaskClass) listView.getItemAtPosition(position); reportId = taskObject.getId()//你的字段; deleteTask(reportId)
  • 你能更新你上面的答案吗?我会投票给你的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2019-09-16
  • 1970-01-01
  • 2016-02-27
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多