【发布时间】: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