-1

I am developing an Android application based on REST API remote data, in which I have made one ListView with two buttons, "Accept" and "Decline".

When the user selects the "Accept" button, certain portions of the JSON property have to change to true and if they select the "Decline" button, certain portions of the JSON property have to change to false and @POST it.

How can I do it?

What I have done so far:

My main activity:

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

}

My adapter class:

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

Have to change this portion of the JSON file:

enter image description here

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

First of all your must change your implementation and use RecyclerView. Also, ViewRefundRequestModel is an Object so you map it in List as I see. You can change the state of the field RefundStatus depending the button. The latter must be done inside onBindViewHolder of the RecyclerView.Adapter. Please search for the implementation of the RecyclerView. To sum up, I assume that you can send your List with the post .

VasFou
  • 1,561
  • 1
  • 22
  • 39