android(Rest Api)中每行中带有接受和拒绝请求按钮的列表视图

Posted

技术标签:

【中文标题】android(Rest Api)中每行中带有接受和拒绝请求按钮的列表视图【英文标题】:List View with Accept and Decline a request Buttons in each Row in android (Rest Api) 【发布时间】:2020-04-14 00:18:29 【问题描述】:

我正在开发一个基于 REST API 远程数据的 android 应用程序,其中我制作了一个带有“接受”和“拒绝”两个按钮的ListView

当用户选择“接受”按钮时,JSON 属性的某些部分必须更改为 true,如果他们选择“拒绝”按钮,JSON 属性的某些部分必须更改为false@POST 它。

我该怎么做?

到目前为止我做了什么:

我的主要活动:

public class ViewRefundRequest extends AppCompatActivity

    ProgressBar progressBarVRR;
    private ListView viewRefundRequestListView;
    RelativeLayout vrrMainLayout;

    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);
        viewRefundRequestListView = (ListView) findViewById(R.id.viewRefundRequestListView);

        //Rest Api call
        allViewRefundRequestData();
    

    private void updateUI(List<ViewRefundRequestModel> VrrModel) 

        AdminViewRefundRequestAdapter adapterVrr = new AdminViewRefundRequestAdapter(VrrModel, this);
        viewRefundRequestListView.setAdapter(adapterVrr);

    

    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)

                    updateUI(response.body());

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

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

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


我的适配器类:

public class AdminViewRefundRequestAdapter extends BaseAdapter 

private List<ViewRefundRequestModel> viewRefundRequestModels;
    private Context context;

    public AdminViewRefundRequestAdapter(List<ViewRefundRequestModel> viewRefundRequestModels, Context context) 
        this.viewRefundRequestModels = viewRefundRequestModels;
        this.context = context;
    

    @Override
    public int getCount() 
        return viewRefundRequestModels.size();
    

    @Override
    public Object getItem(int position)       

        return viewRefundRequestModels.get(position);
    

    @Override
    public long getItemId(int pos) 
        return pos;
    

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 

        if (view == null)
            view = LayoutInflater.from(context).inflate(R.layout.model_view_refund_reques, viewGroup, false);
        

        TextView patient_name = view.findViewById(R.id.patient_name_VRR_Model);
        TextView patient_id = view.findViewById(R.id.patient_id_VRR_Model);
        TextView item_name = view.findViewById(R.id.item_name_VRR_Model);
        TextView category = view.findViewById(R.id.category_VRR_Model);
        TextView quantity = view.findViewById(R.id.quantity_VRR_Model);
        TextView amount = view.findViewById(R.id.amount_VRR_Model);
        TextView discount = view.findViewById(R.id.discount_VRR_Model);
        TextView amount_after_discount = view.findViewById(R.id.amount_after_discount_VRR_Model);
        TextView refund_note = view.findViewById(R.id.refund_note_VRR_Model);

        Button make_decisionBtn_Yes = view.findViewById(R.id.make_decisionBtn01_VRR_Model);
        Button make_decisionBtn_No = view.findViewById(R.id.make_decisionBtn02_VRR_Model);

        make_decisionBtn_Yes.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                //accept the request and make the certain portion of the Json Object property to true
            
        );

        make_decisionBtn_No.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                //accept the request and make the certain portion of the Json Object property to false
            
        );

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

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

        return view;
    

必须更改 JSON 文件的这一部分:

【问题讨论】:

【参考方案1】:

首先你必须改变你的实现并使用RecyclerView。此外, ViewRefundRequestModel 是一个对象,因此您将其映射到List,如我所见。您可以根据按钮更改字段RefundStatus 的状态。后者必须在RecyclerView.AdapteronBindViewHolder 内完成。请搜索RecyclerView的实现。总而言之,我假设您可以将您的列表与帖子一起发送。

【讨论】:

你可以使用retrofit 是的,我确实在使用改造,但我的问题是如何修改我获取的数据并将其从适配器以特定用户 ID 为目标发送或发回?请看图片 示例:***.com/questions/42187238/… 还是没有找到解决办法

以上是关于android(Rest Api)中每行中带有接受和拒绝请求按钮的列表视图的主要内容,如果未能解决你的问题,请参考以下文章

Android java:如何创建 POJO 并将其转换为 Cloud Firestore REST API 可接受的 JSON

接受预付卡 PayPal REST API

Django REST API 接受列表而不是发布请求中的字典

接受 facebook 登录到我的 REST API

Twitter Rest API 不接受 Swift 中令牌的标头

Spring boot REST API 仅接受来自 Angular 前端的调用