2017-04-25 42 views
0

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我想補充的recyclerview複選框的值

我做了什麼,讓我產生按鈕的點擊..但我想的動態複選框

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

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

回答

0

添加的接口在ADA pter

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)); 
} 
+0

其回調shud在適配器類中實現 –

+0

回調接口 –