我想在recyclerview中添加checkbox的值

时间:2017-04-25 10:14:37

标签: android checkbox android-recyclerview

as u all can see ,if i check to my checkbox the price should appear below,beside the makepayment button if i check more than one check box addition should be perform and same way the subtraction

我已经完成了点击按钮给我结果的东西..但我想要动态复选框

Adpter课程:

holder.payment.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        CheckBox cb = (CheckBox) v;
        Buy_Data contact = (Buy_Data) cb.getTag();
        contact.setSelected(cb.isChecked());
        carsList.get(position).setSelected(cb.isChecked());
        Toast.makeText( v.getContext(),"Clicked on Checkbox: " + cb.getText() + " is "
                        + cb.isChecked(), Toast.LENGTH_SHORT).show();
    }
});

recyclerview主要类:

makepayment=(Button)findViewById(R.id.makepayment);
makepayment.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        ArrayList<Buy_Data> stList = ((Mts_Payment_Adapter)adapter).getStudentist();
        int a=0;
        for (int i = 0; i < stList.size(); i++)
        {
            Buy_Data singleStudent = stList.get(i);
            if (singleStudent.isSelected() == true)
            {
                a = a + Integer.parseInt(singleStudent.getPrice());
            }
            else
            {}
        }
        Toast.makeText(Mts_Payment.this, "Selected Students:" + a, 
        Toast.LENGTH_SHORT).show();
        total=(TextView)findViewById(R.id.aaa);
        total.setText(String.valueOf(a));
    }
});

1 个答案:

答案 0 :(得分:0)

在适配器中添加接口

public interface callback {
  void itemSelected(Buy_Data contact);
}

在适配器中为此添加此接口的实例和setter。

Callback callback;

public void setCallback(Callback callback) {
  this.callback = callback;
}

在附加适配器的活动中实现此接口,并使用适配器设置回调,

adapter.setCallback(this);

@Override
void itemSelected(Buy_Data contact) {
  total=(TextView)findViewById(R.id.aaa);
  int totalValue = 0;
  if(!TextUtils.isEmpty(total.getText())) {
    String totalText = total.getText().toString();
    totalValue = Integer.parseInt(totalText);
  }
  totalValue += Integer.parseInt(contact.getPrice());
  total.setText(String.valueOf(totalValue));
}