在这里,我尝试从自定义def sliding(string, k):
return [string[i:i+k] for i in range(len(string)-k+1)]
获取EditText
的值,并添加ListView
的所有值。当列表项为3或小于3时,它运行良好但是当列表视图的项目是4或大于4时,它会抛出空指针异常。我认为它是由于ListView的不可见项而发生的....但是如何我能解决吗???
EditText
logcat的
etPayAmount.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence cs, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
int count = lvRePayment.getCount();// count number of item in listview
double sum = 0;
Toast.makeText(getApplicationContext(),"child of listview: "+count,Toast.LENGTH_SHORT).show();
for (int i = 0; i <count; i++) {
LinearLayout layout = (LinearLayout) lvRePayment.getChildAt(i);// get child from first linear layout
LinearLayout innerLayout = (LinearLayout) layout.getChildAt(1); // get child from inner linear layout
if (innerLayout.getChildCount() > 1) {
EditText editText = (EditText) (innerLayout).getChildAt(1);
try {
double totalPayment = Double.parseDouble(editText.getText().toString());
sum += totalPayment;
}
catch (Exception e) {
}
}
}
etTotalPay.setText(String.valueOf(sum));
pay = sum;
double balance = totalAmount - pay;
etBalance.setText(String.valueOf(balance));
}
});