java Android开发中片段之间传递对象数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43909061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing object array list between fragments in Android development
提问by Arpan Sharma
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:
我正在尝试在 Android 开发中的片段之间传递 arraylist。这是我尝试将事务数组列表传递给另一个片段的部分:
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;
}
The GetAllTransactionAsyncTask.allTransaction
will return a Transaction array list. As for my transaction entity class, I implemented Serializable:
该GetAllTransactionAsyncTask.allTransaction
会返回一个交易数组列表。至于我的事务实体类,我实现了 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.
有任何想法吗?提前致谢。
回答by karthik vishnu kumar
ArrayList<Transaction> transactionList = new ArrayList<>();
pass the transactionList to the bundle
将 transactionList 传递给 bundle
Bundle bundle = new Bundle();
bundle.putSerializable("key", transactionList);
and in the receiving fragment
并在接收片段中
ArrayList<Transaction> transactionList = (ArrayList<Transaction>)getArguments().getSerializable("key");
NOTE: to pass your bean class via bundle you have to implement serializable i.e
注意:要通过 bundle 传递你的 bean 类,你必须实现可序列化,即
YourBeanClass implements Serializable
YourBeanClass 实现了可序列化
回答by Bhupat Bheda
add this gradle in your build.gradle
compile 'com.google.code.gson:gson:2.7'
将此gradle添加到您的 build.gradle
compile 'com.google.code.gson:gson:2.7'
String str = new Gson().toJson(arrayList);
bundle.putStrin("str",str);
and destination fragemnt
和目的地片段
String str = bundle.getString("str");
arrayList = new Gson().fromJson(str,ArrayList.class);
回答by Arpan Sharma
1.You can either pass it by converting into json as specified by @Bhupat.
1.您可以通过转换为@Bhupat指定的json来传递它。
2.Another way is you make your Transaction class parcelable and the use
2.另一种方法是你让你的交易类可打包并使用
b.putParcelableArrayList("list",your_list);
for getting list
获取列表
your_list = getArguments().getParcelableArrayList("list");
EDIT you have too sepcigy type token for getting it back
编辑你有太多 sepcigy 类型的令牌来取回它
transactionlist = new Gson().fromJson(str, new TypeToken<List<type of the list passed>>);
In your case new TypeToken<List<Transaction>>
在你的情况下 new TypeToken<List<Transaction>>
回答by BladeCoder
Instead of implementing Serializable
, you should make your Transaction class implement Parcelable
. Parcelable is the fastest serialization mechanism you can use on Android, it's faster than Serializable or JSON transformation and uses less memory.
Serializable
您应该使您的 Transaction 类实现而不是实现Parcelable
。Parcelable 是您可以在 Android 上使用的最快的序列化机制,它比 Serializable 或 JSON 转换更快,并且使用更少的内存。