How to remove RecyclerView item using Rest API?

I want to remove a RecyclerView item using the breakpoint API. I am using Volley in AdapterClass. When the user clicks on delete the TextView, the RecyclerView item should be deleted using Volley.

Here is the code.

enter image description here}

VechileAdapter.java

public class VechileAdapter extends RecyclerView.Adapter<VechileAdapter.MyViewHolder> {

  private Context mContext;
  public LayoutInflater inflater;
  List<VehicleResponse.VehicleDataBean> vehicleArrayList;
  VehicleResponse.VehicleDataBean current;
  public static final String TAG = AppController.class.getSimpleName();

public VechileAdapter(VehicleActivity context, List<VehicleResponse.VehicleDataBean> vehiclelist) {
    inflater = LayoutInflater.from(context);
    this.mContext = context;
    this.vehicleArrayList=vehiclelist;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout. vechile_card, parent, false);
    return new MyViewHolder(itemView);
}
public void delete(int position) { //removes the row
   // vehicleArrayList.remove(position);
 //   notifyItemRemoved(position);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    current= vehicleArrayList.get(position);
    holder.brandnames.setText(current.getVehicle_brand());
    holder.modelnames.setText(current.getVehicle_model());
    holder.vehicleno.setText(current.getVehicle_number());




@Override
public int getItemCount() {
    return vehicleArrayList.size();
}



 public class MyViewHolder extends RecyclerView.ViewHolder {
   public TextView brandnames, modelnames, vehicleno,  Edit, Delete_vehicle, ;
   public ImageView brandimgs;

   public MyViewHolder(View view) {
        super(view);
        brandnames = (TextView) view.findViewById(R.id.brand_names);
        modelnames = (TextView) view.findViewById(R.id.model_names);
        vehicleno = (TextView) view.findViewById(R.id.tv_vehicleno);
        brandimgs = (ImageView) view.findViewById(R.id.brand_imgs);


        Edit = (TextView) view.findViewById(R.id.edit);
        Delete_vehicle = (TextView) view.findViewById(R.id.tv_delete_vehicle);
        Delete_vehicle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String tag="tag_json_obj";
                String url="...";
                final ProgressDialog pDialog= new ProgressDialog(mContext);
                pDialog.setMessage("Loading");
                pDialog.show();
                JsonObjectRequest jsonObjReq= new JsonObjectRequest(Request.Method.POST,url,null,  new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        pDialog.hide();
                    }
                }
                        ,new  Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error:" + error.getMessage());
                        pDialog.hide();
                    }


                })

                {
                    @Override
                    protected Map<String, String> getParams() {
                        Map<String, String> params = new HashMap<String, String>();

                        params.put("id", current.getId());
                        params.put("user_id", current.getUser_id());
                        return params;
                    }
                };
                AppController.getInstance().addToRequestQueue(jsonObjReq, tag);

            }
        });

}

    }
}

      

+3


source to share


2 answers


Ok what you need to do to achieve this.



+1


source


Just call the delete method in response



public void delete(int position) { //removes the row
   vehicleArrayList.remove(position);
   notifyItemRemoved(position);
}

@Override
public void onResponse(JSONObject response) {
      Log.d(TAG, response.toString());
      pDialog.hide();
      delete(getAdapterPosition());
}

      

0


source







All Articles