Android 添加复选框选择的整数值

Android 添加复选框选择的整数值,android,checkbox,android-recyclerview,adapter,Android,Checkbox,Android Recyclerview,Adapter,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, Strin

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());
            }
        });
    }
    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);
AmountCartActivity.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());
            }
        });
    }
    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);
公共类AmountCartActivity扩展AppCompatActivity实现View.OnClickListener{
@BindView(R.id.总价)
文本视图显示;
SharePreferenceManager SharePreferenceManager;
私人回收站;
列出mydataList;
私用MyAdapter madapter;
捆绑附加;
字符串testName=“”;
字符串testPrice=“”;
字符串totalPrice=“”;
整数计数=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u amount\u cart);
把(这个)绑起来;
sharePreferenceManager=新的sharePreferenceManager(getApplicationContext());
showcenterid(sharePreferenceManager.getUserLoginData(LoginModel.class));
mydataList=新的ArrayList();
/*
*从BUNDLE获取值
* */
extras=getIntent().getExtras();
如果(附加值!=null){
testName=extras.getString(“test_name”);
testPrice=extras.getString(“test_price”);
totalPrice=String.valueOf(extras.getInt(“总价”);
计数=额外的getInt(“序列号”);
//只需在列表中添加您的数据
AmountCartModel mydata=新建AmountCartModel();//模型类的对象
mydata.setTestName(testName);
mydata.setTestPrice(测试价格);
mydata.setTotalPrice(Integer.valueOf(totalPrice));
mydata.setSerialNumber(计数);
添加(mydata);
//totalPrice display.setText(totalPrice);
}
madapter=新的MyAdapter(mydataList);
madapter.setMyDataList(mydataList);
recyclerView=(recyclerView)findViewById(R.id.recyler\u amount\u cart);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(madapter);
RecyclerAdapter.java//AmountCart的RecyclerAdapter

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

    }

}
公共类MyAdapter扩展了RecyclerView.Adapter { 私有列表上下文; 私有列表myDataList; 公共MyAdapter(列表上下文){ this.context=上下文; myDataList=新的ArrayList(); } @凌驾 public ViewHolder onCreateViewHolder(视图组父级,int-viewType) { //替换为您的布局 View=LayoutFlater.from(parent.getContext()).flate(R.layout.amount\u cart\u row,parent,false); 返回新的ViewHolder(视图); } @凌驾 公共无效onBindViewHolder(ViewHolder,int位置){ //在此处将数据设置为布局组件。。 //获得金额 /*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()); } @凌驾 public int getItemCount(){ /*如果(myDataList.size()!=0){ //如果列表不为空,返回列表大小! 返回myDataList.size(); } 返回0*/ 返回myDataList.size(); } 公共void setMyDataList(列表myDataList){ //正在从片段获取列表。 this.myDataList=myDataList; notifyDataSetChanged(); } 公共类ViewHolder扩展了RecyclerView.ViewHolder{ TextView testName、testPrice、textView2; 公共视图持有者(视图项视图){ 超级(项目视图); //itemView.findViewById testName=itemView.findviewbyd(R.id.test\u name\u one); testPrice=itemView.findviewbyd(R.id.test\u price); textView2=itemView.findViewById(R.id.textView2); } } } @凌驾 public void onBackPressed(){ super.onBackPressed(); 星触觉(新) 意图(AmountCartActivity.this,HealthServicesActivity.class)); 完成(); } } 这是我的密码

  • 在这里,我使用HealthActivity,在这个类中,通过使用recycler视图,我在recycler视图中显示了testList。我通过复选框选择testList,将其传递给recycler视图的AmountCartActivity,并且,我计算所选testList的总量,得到结果,并将结果传递给AmountCart活动通过bundle,我在bundle中得到正确的结果,但是,当我试图在textView中显示总量时,它什么也不显示
  • 我的第二个问题是

  • 我正在尝试将序列号显示到我的AmountCartActivity of recycler视图,无论我使用复选框从以前的HealthCartActivity中选择哪个。并且,我已经实现了一些代码,但我不知道如何解决它。请帮助我
  • 第1期

    数据应通过构造函数传递到适配器。问题可能只是向构造函数添加另一个参数:

    public MyAdapter(List<AmountCartModel> context, List<AmountCartModel> myDataList) {
                        this.context = context;
                        myDataList = this.myDataList;
    
                    }
    
    公共MyAdapter(列表上下文,列表myDataList){ this.context=上下文; myDataList=this.myDataList; } 或者

    要将选择支持添加到
    RecyclerView
    实例,请执行以下操作:

    • 确定要使用的选择键类型,然后构建一个
      ItemKeyProvider

    • 实现
      ItemDetailsLookup
      :它允许选择库访问给定动作的RecyclerView项的相关信息