1 回答
TA贡献1775条经验 获得超8个赞
为持有 TextView的Activity/添加回调以获取总成本:Fragment
interface OnCostChangeListener {
void onChange(List<Item> list);
}
实现它:
class MyActivity extends AppCompatActivity implements OnCostChangeListener {
//...
@Override
public void onChange(List<Item> list) {
int sum = 0;
for (Item it : list) {
sum += it.getCost();
}
((TextView) findViewById(R.id.totalCost)).setText(String.valueOf(sum));
}
//...
}
订阅适配器:
private OnCostChangeListener mListener;
public ItemsAdapter(OnCostChangeListener listener){
super(DIFF_CALLBACK);
this.mListener = listener;
}
并在以下位置使用侦听器TextWatcher:
editTextCost3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mItems.get(getAdapterPosition()).setmCost(editTextCost3.getText().toString());
if (mListener != null) {
mListener.onChange(mItems);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
升级版。它应该cost存储Item为数字(整数,在示例中),而不是字符串
添加回答
举报