lundi 24 septembre 2018

how to add integer values in Recycler View

How to add integer values selected by checkbox by using bundle. I am using for loop to add the value and through bundle i am passing the values to activities. I am accessing the list from adapter class. Adapter class is having RecyclerView. And, in AmountCart Activity i am setting the value of the selected checklist value.

MainActivity.class

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


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

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

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

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

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

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

                    finish();

                    break;

AmountCart.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=extras.getString("total_price");


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


        totalPriceDisplay.setText(totalPrice);

    }

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




Aucun commentaire:

Enregistrer un commentaire