将ArrayList <object>从一个Intent传递到另一个Intent

时间:2017-02-23 13:33:31

标签: java android arraylist

我在ActivityA中创建了一个ArrayList,我想将它传递给ActivityB。

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是否需要其他东西?

2 个答案:

答案 0 :(得分:1)

您可以使用serializable或parcelable传递自定义对象的arraylist。

使用以下链接帮助批次

Android: Difference between Parcelable and Serializable?

答案 1 :(得分:0)

您尝试以下操作,CardDetails必须implement Parceable

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

检索

getIntent().getParcelableArrayListExtra("key");