gpt4 book ai didi

java - 调用 objadapter.removeItem(po);仅当列表被删除时

转载 作者:行者123 更新时间:2023-12-01 10:32:25 26 4
gpt4 key购买 nike

我想知道如何实现这一目标。

我在 Activity A 中有一个 listView 。当检测到长按时,它将转到 checkMaxID(ID,iD); 。如果iD最大,则会显示成功,删除列表,最后刷新列表。否则会显示list无法删除并且不会经过objadapter.removeItem(po);

Now what I want to achieve is call objadapter.removeItem(po); only when the list get deleted. If not,
objadapter.removeItem(po); will not be called.

    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(ID,iD);
objadapter.removeItem(po); // only call when list get deleted
RetrieveTotalHours(ID);

}
});

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

{
public void onClick(DialogInterface dialog, int ii) {
dialog.dismiss();
}
}

);
builder.show();
return true;
}
});


public void checkMaxID(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);
// loading.dismiss();
Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}

@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();
}

checkID.php

<?php
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';
}else{
echo 'failed';
}
}
else{
echo 'list cannot be deleted ';
}

}

/* free result set */

}
/* close connection */
mysqli_close($con);
}
?>

最佳答案

您需要在 PHP 脚本中添加一些 json:

<?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{
$json['success'] = false;
$json['message'] = '($row[0] !== $id)';
}
} else{
$json['success'] = false;
$json['message'] = 'select is unsuccessful';
}
/* close connection */
mysqli_close($con);
}
}
?>

并在你的java中解析它:

@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)
}else{
Toast.makeText(getActivity(),
json.getString("message"),
Toast.LENGTH_LONG).show();
}
}catch(JSONException ex){

}
}

关于java - 调用 objadapter.removeItem(po);仅当列表被删除时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35019101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com