mardi 25 septembre 2018

add integer values selected by checkbox

TestListModel.class

    public class TestListModel {

    private String testlist_id;
    private String test_price;
    private String test_name;

    private boolean isSelected;

    public TestListModel(String testlist_id, String test_price, String test_name,boolean isSelected) {
        this.testlist_id = testlist_id;
        this.test_price = test_price;
        this.test_name = test_name;
        this.isSelected = isSelected;
    }

    public String getTestlist_id() {
        return testlist_id;
    }

    public void setTestlist_id(String testlist_id) {
        this.testlist_id = testlist_id;
    }

    public String getTest_price() {
        return test_price;
    }

    public void setTest_price(String test_price) {
        this.test_price = test_price;
    }

    public String getTest_name() {
        return test_name;
    }

    public void setTest_name(String test_name) {
        this.test_name = test_name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
    }

JsonResponse.java

    public class JSONResponse {

    private TestListModel[] result;

    public TestListModel[] getResult() {
        return result;
    }

    public void setResult(TestListModel[] result) {
        this.result = result;
    }
    }

HealthActivity.java

    public class HealthServicesActivity extends AppCompatActivity implements View.OnClickListener {

     /*
    *Api call
    * */
    private RecyclerView recyclerView;
    private ArrayList<TestListModel> data;
    private RecyclerAdapter madapter;

    private Button submitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_services);
        ButterKnife.bind(this);

        sharePreferenceManager = new SharePreferenceManager<>(getApplicationContext());


        submitButton=(Button) findViewById(R.id.submit_button);


     showcenterid(sharePreferenceManager.getUserLoginData(LoginModel.class));


      initViews();


      submitButton.setOnClickListener(this);


    /*
    * On Click Listner
    * */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {

                    case R.id.submit_button:

                        int totalAmount = 0;
                        int totalPrice = 0;
                        String testName = "";
                        String testPrice="";

                        int count = 0;


                        List<TestListModel> stList = ((RecyclerAdapter) madapter)
                                .getTestList();


                       for (int i = 0; i < stList.size(); i++) {
                            TestListModel singleStudent = stList.get(i);

                           //AmountCartModel serialNumber = stList.get(i);


                           if (singleStudent.isSelected() == true) {

                                testName = testName + "\n" + singleStudent.getTest_name().toString();
                                testPrice = testPrice+"\n" + singleStudent.getTest_price().toString();


                                count++;

                                totalAmount = Integer.parseInt(stList.get(i).getTest_price());

                                totalPrice = totalPrice + totalAmount;

                            }
                       }


                        Toast.makeText(HealthServicesActivity.this,
                                "Selected Lists: \n" + testName+ "" + testPrice, Toast.LENGTH_LONG)
                                .show();


                        Intent in= new Intent(HealthServicesActivity.this, AmountCartActivity.class);

                        in.putExtra("test_name", testName);
                        in.putExtra("test_price", testPrice);
                        //in.putExtra("total_price",totalPrice);
                        in.putExtra("total_price", totalPrice);
                        in.putExtra("serialNumber", count);
                        startActivity(in);

                        finish();

                        break;



                    /** back Button Click
                    * */
                    case R.id.back_to_add_patient:
                    startActivity(new Intent(getApplicationContext(), PatientActivity.class));
                    finish();
                    break;


            default:
                break;

        }
    }

    /** show center Id in action bar
     * */
    @Override
    protected void onResume() {
        super.onResume();

    showcenterid(sharePreferenceManager.getUserLoginData(LoginModel.class));
    }
    private void showcenterid(LoginModel userLoginData) {
        centerId.setText(userLoginData.getResult().getGenCenterId());
        centerId.setText(userLoginData.getResult().getGenCenterId().toUpperCase());
        deviceModeName.setText(userLoginData.getResult().getDeviceModeName());
    }


    private void initViews() {
        recyclerView = (RecyclerView)findViewById(R.id.test_list_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }


    private void loadJSON() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(" http://192.168.1.80/aoplnew/api/")
     //                
     .baseUrl("https://earthquake.usgs.gov/fdsnws/event/1/query?")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiInterface request = retrofit.create(ApiInterface.class);
        Call<JSONResponse> call = request.getTestLists();
        call.enqueue(new Callback<JSONResponse>() {


            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

                JSONResponse jsonResponse = response.body();
                data = new ArrayList<>(Arrays.asList(jsonResponse.getResult()));
                madapter = new RecyclerAdapter(data);
                recyclerView.setAdapter(madapter);

            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());
            }
        });
    }

HealthRecyclerAdapter.java

    public class RecyclerAdapter extends 
    RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private ArrayList<TestListModel> android;

    public RecyclerAdapter(ArrayList<TestListModel> android) {
        this.android = android;
    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_row,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, final int position) {

        holder.test_name.setText(android.get(position).getTest_name());
        holder.test_price.setText(android.get(position).getTest_price());




        holder.chkSelected.setChecked(android.get(position).isSelected());

        holder.chkSelected.setTag(android.get(position));

        holder.chkSelected.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                TestListModel contact = (TestListModel) cb.getTag();

                contact.setSelected(cb.isChecked());
                android.get(position).setSelected(cb.isChecked());

                Toast.makeText(
                        v.getContext(),
                        "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
            }
        });
    }



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



    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView test_name;
        private TextView test_price;
        public CheckBox chkSelected;

        public TestListModel testLists;

        public ViewHolder(View itemView) {
            super(itemView);

            test_name = (TextView)itemView.findViewById(R.id.test_name);
            test_price = (TextView)itemView.findViewById(R.id.price_name);
            chkSelected = (CheckBox) itemView.findViewById(R.id.check_box);

        }
    }

    // method to access in activity after updating selection
    public List<TestListModel> getTestList() {
        return android;
    }

AmountCartModel.java

    public class AmountCartModel {


    private String testName;
    private String testPrice;
    private Integer serialNumber;
    private Integer totalPrice;


    public AmountCartModel() {
        this.testName = testName;
        this.testPrice = testPrice;
        this.serialNumber = serialNumber;
        this.totalPrice = totalPrice;
    }


    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    public String getTestPrice() {
        return testPrice;
    }

    public void setTestPrice(String testPrice) {
        this.testPrice = testPrice;
    }

    public Integer getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(Integer serialNumber) {
        this.serialNumber = serialNumber;
    }

    public Integer getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Integer totalPrice) {
        this.totalPrice = totalPrice;
    }
    }

AmountCartActivity.java

    public class AmountCartActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.total_price)
    TextView totalPriceDisplay;

    SharePreferenceManager<LoginModel> sharePreferenceManager;

    private RecyclerView recyclerView;

    List<AmountCartModel> mydataList ;

    private MyAdapter madapter;

    Bundle extras ;
    String testName="";
    String testPrice="";
    String totalPrice= "";

    int counting = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amount_cart);
        ButterKnife.bind(this);


        sharePreferenceManager = new SharePreferenceManager<>(getApplicationContext());


     showcenterid(sharePreferenceManager.getUserLoginData(LoginModel.class));


        mydataList = new ArrayList<>();
        /*
        * Getting Values From BUNDLE
        * */
        extras = getIntent().getExtras();

        if (extras != null) {

            testName = extras.getString("test_name");
            testPrice = extras.getString("test_price");
            totalPrice = String.valueOf(extras.getInt("total_price"));

            counting = extras.getInt("serialNumber");

            //Just add your data in list
            AmountCartModel mydata = new AmountCartModel();  // object of Model Class
            mydata.setTestName(testName );
            mydata.setTestPrice(testPrice);

            mydata.setTotalPrice(Integer.valueOf(totalPrice));

            mydata.setSerialNumber(counting);

            mydataList.add(mydata);

            //totalPriceDisplay.setText(totalPrice);

        }


        madapter=new MyAdapter(mydataList);
        madapter.setMyDataList(mydataList);
        recyclerView = (RecyclerView)findViewById(R.id.recyler_amount_cart);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(madapter);

RecyclerAdapter.java //RecyclerAdapter for AmountCart

     public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> 
    {

        private List<AmountCartModel> context;
        private List<AmountCartModel> myDataList;

        public MyAdapter(List<AmountCartModel> context) {
            this.context = context;
            myDataList = new ArrayList<>();
        }



       @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    {

            // Replace with your layout
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.amount_cart_row, parent, false);
            return new ViewHolder(view);
        }


        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // Set Your Data here to yout Layout Components..

            // to get Amount
           /* myDataList.get(position).getTestName();
            myDataList.get(position).getTestPrice();*/


            holder.testName.setText(myDataList.get(position).getTestName());

       holder.testPrice.setText(myDataList.get(position).getTestPrice());

       holder.textView2.setText(myDataList.get(position).getSerialNumber());

        }


        @Override
        public int getItemCount() {
            /*if (myDataList.size() != 0) {
                // return Size of List if not empty!
                return myDataList.size();
            }
            return 0;*/
            return myDataList.size();
        }


        public void setMyDataList(List<AmountCartModel> myDataList) {
            // getting list from Fragment.
            this.myDataList = myDataList;
            notifyDataSetChanged();
        }


        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView testName,testPrice,textView2;


            public ViewHolder(View itemView) {
                super(itemView);
                // itemView.findViewById

                testName=itemView.findViewById(R.id.test_name_one);
                testPrice=itemView.findViewById(R.id.test_price);
                textView2=itemView.findViewById(R.id.textView2);
            }
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();

        startActivity(new 
     Intent(AmountCartActivity.this,HealthServicesActivity.class));
        finish();

    }

}

This is my code.

  1. Here I am taking HealthActivity and in this class by using recycler view I have displayed testList in recycler view. I am passing testList whichever I am selecting through checkbox to AmountCartActivity of recycler View, And, I am calculating total amount of the selected testList and I am getting the result and that result I am passing to the AmountCart Activity through bundle and I am getting correct result in bundle, but, when I am trying to display total amount in a textView its showing me nothing.

And, my second problem is,

  1. I am trying to display serial number to to my AmountCartActivity of recycler view whichever I am selecting from previous HealthCartActivity using checkbox. And, I have implemented some code but I am not getting how to solve it. please help me.



Aucun commentaire:

Enregistrer un commentaire