(Rest Api) Recycler View with Accept and Decline a request Buttons in each Row in android

我与影子孤独终老i 提交于 2020-01-16 08:06:49

问题


I am developing an android application based on Rest Api remote data, in which I have made one RecyclerView. I have added 2 buttons with each row in RecyclerView. These 2 buttons are Accept and Decline. When user selects accept button certain portion of the JSON Property have to change to True and if selects decline button certain portion of the JSON Property have to change to false and @POST it. How can I do it?

I’m new in Rest Api with RecyclerView, so if you know the solution please help, thanks.

What i have been done so far:

My main activity:

public class ViewRefundRequest extends AppCompatActivity{

    private RecyclerView viewRefundRequestRecylcerView;
    private RecyclerView.LayoutManager layoutManager;
    private AdminViewRefundRequestAdapter adapter;
    List<ViewRefundRequestModel> vrrList;
    RelativeLayout vrrMainLayout;
    AdminViewRefundRequestAdapter.RecyclerViewClickListener listener;
    ProgressBar progressBarVRR;

    ApiService serviceVRR;
    TokenManager tokenManagerVrr;

    Call<List<ViewRefundRequestModel>> callViewRefundRequestData;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_refund_request);

        tokenManagerVrr = TokenManager.getInstance(getSharedPreferences("prefs", MODE_PRIVATE));
        serviceVRR = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManagerVrr);

        progressBarVRR = (ProgressBar) findViewById(R.id.viewRefundRequestProgressBar);
        viewRefundRequestRecylcerView = (RecyclerView) findViewById(R.id.viewRefundRequestRecylcerView);

        layoutManager = new LinearLayoutManager(this);
        viewRefundRequestRecylcerView.setLayoutManager(layoutManager);

        listener = new AdminViewRefundRequestAdapter.RecyclerViewClickListener() {
            @Override
            public void onRowClick(View view, int position) {

            }

            @Override
            public void onButtonYesClick(View view, int position) {
                int acceptStatusCode = 110;

                final int id = vrrList.get(position).getId();
                //final int statusCode = vrrList.get(position).getRefundStatus();               
                acceptRequest(id, acceptStatusCode);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onButtonNoClick(View view, int position) {

            }
        };

        //Rest Api call
        allViewRefundRequestData();
    }


    private void allViewRefundRequestData() {
        progressBarVRR.setVisibility(View.VISIBLE);
        callViewRefundRequestData = serviceVRR.getAllViewRefundRequest();
        callViewRefundRequestData.enqueue(new Callback<List<ViewRefundRequestModel>>() {
            @Override
            public void onResponse(@NotNull Call<List<ViewRefundRequestModel>> call, @NotNull Response<List<ViewRefundRequestModel>> response) {
                progressBarVRR.setVisibility(View.GONE);

                if (response.isSuccessful() && response.body() != null){

                    vrrList = response.body();
                    adapter = new AdminViewRefundRequestAdapter(vrrList, ViewRefundRequest.this, listener);
                    viewRefundRequestRecylcerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }else {

                    if (response.code() == 401) {
                        startActivity(new Intent(ViewRefundRequest.this, LoginActivity.class));
                        finish();
                        tokenManagerVrr.deleteToken();
                        Toast.makeText(ViewRefundRequest.this, "User session expired, Login again", Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(@NotNull Call<List<ViewRefundRequestModel>> call, @NotNull Throwable t) {
                progressBarVRR.setVisibility(View.GONE);

                Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Network Status: " + t.getMessage(), Snackbar.LENGTH_LONG);
                View snackbarView = snackbar.getView();
                snackbarView.setBackgroundColor(Color.parseColor("#f5003d"));
                TextView tv = (TextView) snackbarView.findViewById(R.id.snackbar_text);
                tv.setTextColor(Color.WHITE);
                snackbar.show();
            }
        });
    }

    public void acceptRequest(final int id, final int statusCode){
        Call<ViewRefundRequestModel> callAccepted = serviceVRR.acceptRefundRequest(id, statusCode);

        callAccepted.enqueue(new Callback<ViewRefundRequestModel>() {
            @Override
            public void onResponse(Call<ViewRefundRequestModel> call, Response<ViewRefundRequestModel> response) {

                Snackbar snackbar = Snackbar.make(vrrMainLayout, "Accepted", Snackbar.LENGTH_SHORT);
                View snackbarView = snackbar.getView();
                snackbarView.setBackgroundColor(Color.parseColor("#5ec639"));
                TextView tv = (TextView) snackbarView.findViewById(R.id.snackbar_text);
                tv.setTextSize(16);
                tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
                tv.setTextColor(Color.WHITE);
                snackbar.show();
            }

            @Override
            public void onFailure(Call<ViewRefundRequestModel> call, Throwable t) {

            }
        });
    }

    public void declineRequest(final int id, Integer statusCode, String noteRefund){
        Call<ViewRefundRequestModel> callDeclined = serviceVRR.cancelRefundRequest(id, statusCode, noteRefund);

        callDeclined.enqueue(new Callback<ViewRefundRequestModel>() {
            @Override
            public void onResponse(Call<ViewRefundRequestModel> call, Response<ViewRefundRequestModel> response) {                
            }

            @Override
            public void onFailure(Call<ViewRefundRequestModel> call, Throwable t) {               
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (callViewRefundRequestData != null) {
            callViewRefundRequestData.cancel();
            callViewRefundRequestData = null;
        }
    }

}

My adapter class:

public class AdminViewRefundRequestAdapter extends BaseAdapter {

private List<ViewRefundRequestModel> viewRefundRequestModels;
    List<ViewRefundRequestModel> viewRefundRequestModels;
    private Context context;
    private RecyclerViewClickListener mListener;

    public AdminViewRefundRequestAdapter(List<ViewRefundRequestModel> viewRefundRequestModels, Context context, RecyclerViewClickListener listener) {
        this.viewRefundRequestModels = viewRefundRequestModels;
        this.context = context;
        this.mListener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.model_view_refund_reques, parent, false);
        return new MyViewHolder(view, mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        final ViewRefundRequestModel thisModelResponse = viewRefundRequestModels.get(position);

        Patient patient = thisModelResponse.getPatient();
        String patientName= patient.getFirstName()+" "+patient.getLastName();

        Item item =thisModelResponse.getItem();
        String itemName= item.getName();

        ItemCategory itemCategory = item.getItemCategory();
        String itemCategoryName = itemCategory.getName();

        holder.patient_name.setText(patientName);
        holder.patient_id.setText(Integer.toString(thisModelResponse.getPatientID()));
        holder.item_name.setText(itemName);
        holder.category.setText(itemCategoryName);
        holder.quantity.setText(Integer.toString(thisModelResponse.getServiceQuantity()));
        holder.amount.setText(Double.toString(thisModelResponse.getServiceActualPrice()));
        holder.discount.setText(Double.toString(thisModelResponse.getDiscount()));
        holder.amount_after_discount.setText(Double.toString(thisModelResponse.getServiceListPrice()));
        holder.refund_note.setText(thisModelResponse.getRefundNote());

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private RecyclerViewClickListener mListener;
        private TextView patient_name, patient_id, item_name,
                category, quantity, amount, discount, amount_after_discount, refund_note;

        private Button make_decisionBtn_Yes, make_decisionBtn_No;
        private RelativeLayout row_container_vrr;


        public MyViewHolder(@NonNull View itemView, RecyclerViewClickListener listener) {
            super(itemView);
            patient_name = itemView.findViewById(R.id.patient_name_VRR_Model);
            patient_id = itemView.findViewById(R.id.patient_id_VRR_Model);
            item_name = itemView.findViewById(R.id.item_name_VRR_Model);
            category = itemView.findViewById(R.id.category_VRR_Model);
            quantity = itemView.findViewById(R.id.quantity_VRR_Model);
            amount = itemView.findViewById(R.id.amount_VRR_Model);
            discount = itemView.findViewById(R.id.discount_VRR_Model);
            amount_after_discount = itemView.findViewById(R.id.amount_after_discount_VRR_Model);
            refund_note = itemView.findViewById(R.id.refund_note_VRR_Model);

            make_decisionBtn_Yes = itemView.findViewById(R.id.make_decisionBtn01_VRR_Model);
            make_decisionBtn_No = itemView.findViewById(R.id.make_decisionBtn02_VRR_Model);
            row_container_vrr = itemView.findViewById(R.id.row_container_vrr);

            mListener = listener;
            row_container_vrr.setOnClickListener(this);
            make_decisionBtn_Yes.setOnClickListener(this);
            make_decisionBtn_No.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.row_container_vrr:
                    mListener.onRowClick(row_container_vrr, getAdapterPosition());
                    break;
                case R.id.make_decisionBtn01_VRR_Model:
                    mListener.onButtonYesClick(make_decisionBtn_Yes, getAdapterPosition());
                    break;
                case R.id.make_decisionBtn02_VRR_Model:
                    mListener.onButtonNoClick(make_decisionBtn_No, getAdapterPosition());
                    break;
                default:
                    break;
            }
        }
    }

    public interface RecyclerViewClickListener {
        void onRowClick(View view, int position);
        void onButtonYesClick(View view, int position);
        void onButtonNoClick(View view, int position);
    }
}

Where did I go wrong?

Or should I follow another approach?

If I have to then what is it?


回答1:


You can do like this first create a Pojo class like

class SendDataModel{

    private String email;
    private Name name;
    private String password;

    public String getEmail ()
    {
        return email;
    }

    public void setEmail (String email)
    {
        this.email = email;
    }

    public Name getName ()
    {
        return name;
    }

    public void setName (Name name)
    {
        this.name = name;
    }

    public String getPassword ()
    {
        return password;
    }

    public void setPassword (String password)
    {
        this.password = password;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [email = "+email+", name = "+name+", password = "+password+"]";
    }
}

and other Pojo Class like

class Name{
    private String lastName;

    private String firstName;

    public String getLastName ()
    {
        return lastName;
    }

    public void setLastName (String lastName)
    {
        this.lastName = lastName;
    }

    public String getFirstName ()
    {
        return firstName;
    }

    public void setFirstName (String firstName)
    {
        this.firstName = firstName;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [lastName = "+lastName+", firstName = "+firstName+"]";
    }
}

and set your first and last name like

Name name = new Name();
    name.setFirstName();
    name.setLastName();
SendDataModel sendDatamodel=new SendDataModel();
    sendDatamodel.setName(name);
    sendDatamodel.setEmail("yourEmail")
    sendDatamodel.setPassword("yourPassword"); 

and pass your sendDatamodel to your request.

Call<RegisterResponseModel> res = apiService.register(sendDatamodel);
res.enqueue(new Callback<RegisterResponseModel>() {
    @Override
    public void onResponse(Call<RegisterResponseModel> call, 
    Response<RegisterResponseModel> response) {

    }

    @Override
    public void onFailure(Call<RegisterResponseModel> call, Throwable t) 
    {
        // Log error here since request failed
        Log.e(TAG, t.toString());
    }
});



回答2:


you have to set clickListener in onBindViewHolder like

@Override public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
    ...
    holder.row_container_vrr.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View v) {
           mListener.onRowClick(v, position);
       }
    );
    holder.make_decisionBtn_Yes.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View v) {
           mListener.onButtonYesClick(v, position);
       }
    );
    holder.make_decisionBtn_No.setOnClickListener(new View.OnClickListener(){
       @Override
       public void onClick(View v) {
           mListener.onButtonNoClick(v, position);
       }
    );
}


来源:https://stackoverflow.com/questions/59560924/rest-api-recycler-view-with-accept-and-decline-a-request-buttons-in-each-row-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!