我想收集String []数字项目。一个例子:这是我的代码:
public class test extends Activity {
String[] numbers = {
"4",
"7",
"24",
"77",
"98"
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = "collect String[] numbers items";
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(str);
}
}
答案 0 :(得分:2)
你可以这样试试,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int total = 0;
for (int i = 0; i < numbers.length; i++) {
if(TextUtils.isDigitsOnly(numbers[i])) {
total += Integer.parseInt(numbers[i]);
}
}
//String str = "collect String[] numbers items";
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(String.valueOf(total);
}