2017-08-01 30 views
-1

我想收集String []數字項。一個例子:這是我的代碼:Android - Collect String Array items

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

嘿@android_Muncher是我黑曼巴蛇 – BlackMamba2

+0

歡迎的StackOverflow!請編輯您的問題並澄清您的問題。你想做什麼?什麼不工作? – ventiseis

+0

你想將字符串數組設置爲textview texto? @ BlackMamba2 –

回答

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

    } 
+0

謝謝。這是完美的。 – BlackMamba2