我创建了一个删除地址类
码
public class DeleteAddress {
boolean result = false;
String addressid;
public boolean delAddress(Context context, final int id){
addressid = Integer.toString(id);
StringRequest stringRequest = new StringRequest(
Request.Method.POST,
Constants.URL_DELETE_ADDRESS,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getBoolean("error")){
result = false;
}
else {
result = true;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
result = false;
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id", addressid);
return params;
}
};
RequestHandler.getInstance(context).addToRequestQueue(stringRequest);
return result;
}
}
地址适配器的代码
public class AddressAdapter extends RecyclerView.Adapter<AddressHolder>{
private ArrayList<Address> dataList;
Context context;
int id;
DeleteAddress deleteAddress = new DeleteAddress();
public AddressAdapter(Context context, ArrayList<Address> list){
this.context = context;
this.dataList= list;
}
@NonNull
@Override
public AddressHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.row, parent, false);
return new AddressHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AddressHolder holder, int position) {
holder.bind(dataList.get(position));
id = holder.getAddressId(dataList.get(position));
holder.deleteAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setMessage("Are you sure you want to delete this address?");
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//dummy
arg0.cancel();
//System.out.println(id); working fine
if(deleteAddress.delAddress(context, id)){
Toast.makeText(context, "Address record deleted!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "Some error occurred!", Toast.LENGTH_SHORT).show();
}
Intent i = new Intent(context, AddressListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int i) {
arg0.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
}
所以让我解释一下我要做什么。
我做了一个布局,在“地址”活动中显示地址,并且正在显示来自mysql php后端的数据。此活动运行正常。我在回收者视图中添加了两个项目,我们可以使用它们来编辑和删除特定的地址记录。编辑按钮可以正常工作,但删除不起作用。
如果我单击删除按钮,我会收到吐司消息,发生了一些错误。但是,如果我测试按钮2-3次,我会收到相同的消息,但记录将被删除。Web服务运行正常,因此没有问题。
logcat中没有错误。
我不知道怎么回事。有人可以帮忙吗?需要更多的代码部分我可以对其进行更新...
编辑:我发现了错误。在deleteaddress类中,由于某种原因,结果变量的值在String请求方法中未得到更新。我试图从logcat的Web服务中打印消息,它表明该地址已被删除,并且我检查数据库也正在更新,但是我仍然收到吐司消息,指出发生了一些错误。
onResponse(String response)
是异步方法
您result
在回调函数中进行更新。该方法delAddress
始终返回false。(它true
在第二次执行时返回,因为result
后来被更新了)
您的代码应更改为:
制作新界面
public interface CallbackClass {
void onSuccess();
void onFail(String message);
}
更改
public boolean delAddress(Context context, final int id)
至
public void delAddress(Context context, final int id, CallbackClass callback)
然后
result = false;
可能像
callback.fail("error");
和
result = true;
可能像
callback.success();
至少
if(deleteAddress.delAddress(context, id)){
Toast.makeText(context, "Address record deleted!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Some error occurred!", Toast.LENGTH_SHORT).show();
}
至
deleteAddress.delAddress(context, id, new CallbackClass(){
void onSuccess(){
Toast.makeText(context, "Address record deleted!", Toast.LENGTH_SHORT).show();
}
void onFail(String message){
Toast.makeText(context, "Some error occurred!", Toast.LENGTH_SHORT).show();
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句