4

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;
}

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.

5
  • try it bundle.putParcelableArrayList("transactionlist", GetAllTransactionAsyncTask.allTransaction); then get receive it Commented May 11, 2017 at 7:29
  • I did it but it is incompatible. That's why I am commenting out that line :) Commented May 11, 2017 at 7:30
  • add parceable plug in your android studio Commented May 11, 2017 at 7:31
  • Add your Transaction class in an arraylist like this ArrayList<Transaction> transactionList = new ArrayList<>(); and then pass it via bundle, new Bundle().putSerializable("Key", transactionList); Commented May 11, 2017 at 7:35
  • @karthikvishnukumar Sorry but how do I retrieve it at the destination fragment? Commented May 11, 2017 at 7:37

4 Answers 4

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

pass the transactionList to the 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

YourBeanClass implements Serializable

Sign up to request clarification or add additional context in comments.

Comments

4

add this gradle in your 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);

1 Comment

After I changed it to "Bundle bundle = getArgument()" in destination fragment, I am getting this error message: java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to Entity.Transaction
1

1.You can either pass it by converting into json as specified by @Bhupat.

2.Another way is you make your Transaction class parcelable and the use

 b.putParcelableArrayList("list",your_list);

for getting list

your_list = getArguments().getParcelableArrayList("list");

EDIT you have too sepcigy type token for getting it back

 transactionlist = new Gson().fromJson(str,  new TypeToken<List<type of the list passed>>);

In your case new TypeToken<List<Transaction>>

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.