(休息 Api)在 android 的每一行中带有接受和拒绝请求按钮的回收器视图

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

我正在开发一个基于 Rest Api 远程数据的 android 应用程序,我已经在其中制作了一个 RecyclerView。我在 RecyclerView 中的每一行添加了 2 个按钮。这 2 个按钮是接受和拒绝。当用户选择接受按钮时,JSON 属性 的某些部分必须更改为 True,如果选择拒绝按钮,JSON 属性 的某些部分必须更改为 false 并且 @POST它。我该怎么做?

我是 Rest Api RecyclerView 的新手,所以如果您知道解决方案请帮忙,谢谢。

到目前为止我做了什么:

我的主要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;
        }
    }

}

我的适配器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);
    }
}

我哪里错了?

或者我应该采用另一种方法吗?

如果我必须那么它是什么?

您必须像

一样在 onBindViewHolder 中设置 clickListener
@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);
       }
    );
}

你可以这样做,首先创建一个 Pojo class 就像

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+"]";
    }
}

和其他 Pojo Class 喜欢

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+"]";
    }
}

并将您的名字和姓氏设置为

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

并将您的 sendDatamodel 传递给您的请求。

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());
    }
});