2013-10-01 21 views
0

我正在使用這種方法來獲取用戶帳戶,它工作正常。現在我想存儲它一個變量,我必須打印用戶帳戶。我該怎麼做?如何在android中存儲帳戶數組列表?

private String getFirstAccount() { 
      Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
      Account[] accounts = AccountManager.get(HomeScreen.this).getAccounts(); 

      for (Account account : accounts) { 
       if (emailPattern.matcher(account.name).matches()) { 
        String possibleEmail = account.name; 

        return possibleEmail; 
       } 
      } 
      return null; 
     } 

例如:如果我得到三個用戶帳戶,我想將三個帳戶存儲在變量中並打印出來。

回答

2
 ArrayList<Account> tempList = new ArrayList<Account>(); 
     for (Account account : accounts) { 
      if (emailPattern.matcher(account.name).matches()) { 
       templist.add(account.name); 
      } 
     } 
     System.out.println(tempList); 
     return tempList; //Returning an empty list is better than returning null 
+0

我在templist.add(account.name)上收到錯誤; – Deepak

+0

它應該是templist.add(帳戶); –

+0

不,Arraylist應該是一個字符串 – Deepak

相關問題