Remove items from RecyclerView

Jyotishgher Astrology
By -
0

 Remove items from RecyclerViewAdapter in Android

To remove the verified OTP complaint from the RecyclerView, you can directly remove the corresponding item from the adapter's data list and notify the adapter. Here's the updated code with that logic integrated:

Remove items from RecyclerView


Let's Take an example for an OTP inside 

RecyclerViewAdapter

private void verify_OTP(String compNo, String serStationCode, String otp, String rating, String feed, String close_by_emp, String contactPhone) {
    final ProgressDialog loading = ProgressDialog.show(context, "Verifying", "Please wait...", false, false);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, electrical_verify_OTP_technician,
            response -> {
                loading.dismiss();
                Log.d("verify_OTP-", "Response: " + response);

                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    int msg = jsonResponse.optInt("success", 0);
                    String serverMessage = jsonResponse.optString("message", "Unknown response from server");

                    if (msg == 1) {
                        // Remove the verified complaint from the list
                        for (int i = 0; i < getDataAdapter.size(); i++) {
                            if (getDataAdapter.get(i).getComp_no().equals(compNo)) {
                                getDataAdapter.remove(i);
                                break;
                            }
                        }
                        notifyDataSetChanged(); // Refresh the RecyclerView

                        // Show success dialog
                        new AlertDialog.Builder(context)
                                .setTitle("OTP Status")
                                .setMessage(serverMessage)
                                .setPositiveButton("OK", (dialog, which) -> dialog.dismiss())
                                .show();

                    } else {
                        Toast.makeText(context, serverMessage.equals("Invalid OTP") ? "Invalid OTP" : serverMessage, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "Unexpected server response", Toast.LENGTH_LONG).show();
                }
            },
            error -> {
                loading.dismiss();
                Toast.makeText(context, "Error submitting rating. Please try again.", Toast.LENGTH_LONG).show();
            }) {
        @Override
        protected Map<String, String> getParams() {

            Map<String, String> params = new HashMap<>();
            params.put("complaint_no", compNo);
            params.put("contactPhone", contactPhone);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(context, new HurlStack(null, getSSLSocketFactory()));
    requestQueue.add(stringRequest);
}

Key Changes:

  1. Remove Verified Complaint: Iterated through getDataAdapter to find and remove the complaint based on compNo.
  2. Notify Adapter: Called notifyDataSetChanged() to refresh the RecyclerView after the item removal.

This approach ensures that the RecyclerView stays in sync with the updated data.

Post a Comment

0Comments

Post a Comment (0)