在Android开发中的片段之间传递对象数组列表 [英] Passing object array list between fragments in Android development

查看:430
本文介绍了在Android开发中的片段之间传递对象数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android开发中的片段之间传递arraylist.这是我尝试将事务数组列表传递给另一个片段的部分:

I am trying to pass arraylist between fragments in Android development. This is the part where I tried to pass Transaction array list to another fragment:

switch (menuItem.getItemId()){

                case R.id.expenses:
                    final ExpenseActivity expenseFragment = new ExpenseActivity();
                    new GetAllTransactionAsyncTask(
                            new GetAllTransactionAsyncTask.OnRoutineFinished() {
                                public void onFinish() {
                                    FragmentTransaction expsenseTransaction = getSupportFragmentManager().beginTransaction();
                                    Bundle bundle = new Bundle();
                                    //bundle.putParcelableArrayList("transactionlist", GetAllTransactionAsyncTask.allTransaction);
                                    //bundle.putString("transactionlist", GetAllTransactionAsyncTask.allTransaction);
                                    expenseFragment.setArguments(bundle);
                                    expsenseTransaction.replace(R.id.frame,expenseFragment);
                                    expsenseTransaction.commit();
                                }
                            }).execute(session_accountID);


                    return true;
}

GetAllTransactionAsyncTask.allTransaction将返回一个事务数组列表.至于我的交易实体类,我实现了Serializable:

The GetAllTransactionAsyncTask.allTransactionwill return a Transaction array list. As for my transaction entity class, I implemented Serializable:

import java.io.Serializable;

@SuppressWarnings("serial")
public class Transaction implements Serializable{
...
}

我不确定如何在片段之间实际传递对象数组列表.我注释掉了这两行,因为它们是不兼容的类型.

I not sure how do I actually pass an object array list between fragments. I commented out the two lines as they are incompatible type.

有什么想法吗?预先感谢.

Any ideas? Thanks in advance.

推荐答案

ArrayList<Transaction> transactionList = new ArrayList<>();

将transactionList传递到捆绑软件中

pass the transactionList to the bundle

Bundle bundle = new Bundle();
bundle.putSerializable("key", transactionList);

以及接收片段中

ArrayList<Transaction> transactionList = (ArrayList<Transaction>)getArguments().getSerializable("key");

注意:要通过捆绑包传递Bean类,您必须实现可序列化,即

NOTE: to pass your bean class via bundle you have to implement serializable i.e

YourBeanClass实现可序列化

YourBeanClass implements Serializable

这篇关于在Android开发中的片段之间传递对象数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆