i created a delete address class
code
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;
}
}
the code for address adapter
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();
}
}
so let me explain what i am trying to do.
I have made a layout that displays address in Address activity and i am displaying data from mysql php backend. this activity is working fine. i added two items in the recycler view using which we can edit and delete a particular address record. edit button is working fine but delete isn't working.
If i click on delete button i get toast message some error occured. but if i test the button 2-3 times i get the same message but the record gets deleted.
web services are working fine so no issues with that.
there are no errors in logcat.
I don't know what is going on. can someone help. need any more portion of code i can update it...
EDIT:
i have caught the error. in the deleteaddress class for some reason the value of result variable is not getting updated inside the String request method. i tried to print the message from the web service in the logcat and it shows that address is getting deleted and i checked the database is also getting updated but still i get the toast message that some error occured.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…