2017-02-23 51 views
1

我在ActivityA中創建了一個ArrayList,我想將它傳遞給ActivityB。從一個Intent傳遞ArrayList <Object>到另一個

ActivityA:

ArrayList<CardDetails> cardDetailsToPass = new ArrayList<>(); 

     if (v.getId() == balance.getId()){ 
      for(CardDetails cardDetails : cardDetailsArrayList){ 
       int clientID = cardDetails.getClientID(); 
       int cardID = cardDetails.getCardID(); 
       int cardNo = cardDetails.getCardNo(); 
       int balance = cardDetails.getCardBalance(); 
       int availableBalance = cardDetails.getCardAvailableBalance(); 
       int limit = cardDetails.getCardLimit(); 
       int expDate = cardDetails.getExpDate(); 
       String status = cardDetails.getStatus(); 

       cardDetailsToPass.add(new CardDetails(clientID,cardID,cardNo,balance,availableBalance,limit,expDate,status)); 

       Log.d("Adapter","clientID    "+cardDetails.getClientID()); 
       Log.d("Adapter","cardID     "+cardDetails.getCardID()); 
       Log.d("Adapter","cardNo     "+cardDetails.getCardNo()); 
       Log.d("Adapter","cardBalance   "+cardDetails.getCardBalance()); 
       Log.d("Adapter","cardAvailableBalance "+cardDetails.getCardAvailableBalance()); 
       Log.d("Adapter","cardLimit    "+cardDetails.getCardLimit()); 
       Log.d("Adapter","cardExpDate   "+cardDetails.getExpDate()); 
       Log.d("Adapter","cardStatus    "+cardDetails.getStatus()); 

      } 

      Intent intent = new Intent(context, CardStatementActivity.class); 
      intent.putExtra("cardDetailsToPass",cardDetailsToPass); 
      context.startActivity(intent); 

ActivityB:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_card_statement); 



    Bundle extras = getIntent().getExtras(); 
    if(extras != null){ 
     //get the ArrayList here 
    } 
} 

我已經試過這樣做,但沒有成功的一種方法。傳遞對象的ArrayList是否需要別的東西?

回答

0

您嘗試以下,你CardDetails必須implement Parceable

intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list); 
startActivity(intent); 

檢索

getIntent().getParcelableArrayListExtra("key"); 
相關問題