1

I want to pass an Arraylist (SearchModl is a class) from one fragment to another. i am using Bundle to do this.But when ever I get the bundle in other fragment,i am getting null value

SearchModel

public class SearchModel implements Serializable {

    private String code,category, minExp, maxExp, postedOn, counts, applied, position, desc, type, hour, status, expiryDate, address, gender, religion, summary, requestId, requestorId;

    public SearchModel(String code,String category, String minExp, String maxExp, String postedOn, String counts, String applied, String position, String desc, String type, String hour, String status, String expiryDate, String address, String gender, String religion, String summary, String requestId, String requestorId) {
        this.code=code;
        this.category = category;
        this.minExp = minExp;
        this.maxExp = maxExp;
        this.postedOn = postedOn;
        this.counts = counts;
        this.applied = applied;
        this.position = position;
        this.desc = desc;
        this.type = type;
        this.hour = hour;
        this.status = status;
        this.expiryDate = expiryDate;
        this.address = address;
        this.gender = gender;
        this.religion = religion;
        this.summary=summary;
        this.requestId = requestId;
        this.requestorId = requestorId;
    } 

Fragment A

for (int i = 0; i < tableArray.length(); i++) {
                                        JSONObject table = tableArray.getJSONObject(i);
                                        data = new SearchModel(table.getString("Job_Code"), table.getString("Job_Category"), table.getString("Min_Exp"), table.getString("Max_Exp"), table.getString("Posted_On"), table.getString("Candidate_Counts"), table.getString("Applications"), table.getString("No_Of_Pos"), table.getString("Job_Desc"), table.getString("Job_Type"), table.getString("Job_Hours"), table.getString("Job_Status"), table.getString("Job_Exp_Date"), table.getString("Address"), table.getString("Gender_Name"), table.getString("Religion_Name"), table.getString("Exp_Summary"), table.getString("IJob_Request_ID"), table.getString("Requestor_Name"));
                                        values.add(data);

                                    }

//                                    getArrayList.getArray(values);


                                    bundle.putSerializable("array", (java.io.Serializable) values);

                                }
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }
                        CommonFunctions.showProgress(getActivity(), "Please Wait...", false);
                        fragmentTransaction = getFragmentManager().beginTransaction();
                        SearchJobList searchJobList = new SearchJobList();
                        searchJobList.setArguments(bundle);
                        fragmentTransaction.replace(R.id.header_container, searchJobList, "searchJob").commit();
                    }
                }); 

Second Fragment

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        data = new ArrayList<SearchModel>();
        Bundle bundle = getArguments();
        data = (List<SearchModel>) bundle.getSerializable("array");
        getArrayList = new GetArrayList() {
            @Override
            public void getArray(List<SearchModel> listValues) {
                data = listValues;
                jobAdapter = new SearchJobAdapter(getActivity(), data);
                setListAdapter(jobAdapter);
            }
        };

    } 
user3811519
  • 11
  • 1
  • 2
  • Its depends on ur logic. One way u can create model class object in ur activity class. Put two function in ur Activity class for setting and getting data. In fragment A fill the data. In fragment B u can call get method fetching data. The other through communicator method. – user2851150 Jul 09 '14 at 06:23

2 Answers2

1

You can simply keep the ArrayList in your Activity where it is available to both Fragments.

Dynamically, you create a public method in your fragment and after you create the fragment, you pass the List via a setter.

 YourActivity extends Activity {

   void createFragment(){
        YourFragment yourFragment = Fragment.getInstance();
        yourFragment.setList(models);
  }

}

YourFragment extends Fragment {
  private List<SearchModel> models; 

  public void setList(List<SearchModel> models){
        models = models;
  }
}

Statically, not the best solution either:

YourActivity extends Activity {
    static ArrayList<SearchModel> models;


}

YourFragment extends Fragment {

  public void doSomething(){
        List<SearchModel> model = YourActivity.models;
  }
}

Implementing PArcelable would be the best solution though:

http://developer.android.com/reference/android/os/Parcelable.html There is a solution right here: Android ArrayList<MyObject> pass as parcelable

Community
  • 1
  • 1
vandus
  • 3,248
  • 3
  • 30
  • 44
  • I have edited my answer for you to see the possibilities. I hope it is clear :) – vandus Jul 09 '14 at 06:31
  • The same way as in the first one. If you want to keep the two lists consistent, then you dont need to create a local List in the first fragment, simply maintain the one in your Activity from both fragments. So for example when adding, you would do YourActivity.models.add(Object). If you want to just send the lists to the fragments without keeping them consistent, you just pass them as arrays of parcelable objects to the Bundle. – vandus Jul 09 '14 at 06:38
  • Are you sure the list itself is not null? Weren´t you implementing Serializable before? – vandus Jul 09 '14 at 06:44
  • @vandus how can you retrieve the list from the asset folder by using a simple [activity](http://stackoverflow.com/questions/24255893/getting-hash-value-instead-of-the-complete-file) – user285oo6 Jul 09 '14 at 06:49
  • by what means do you have the list stored in the assets folder? – vandus Jul 09 '14 at 07:00
0

1.Implement a singleton class as the example to pass object. (Hopefully you know what's singleton, I you don't, add comment to tell me.

class ExampleClass {
    private ExampleClass () {}

    static ExampleClass obj = nil;
    public ExampleClass instance() {
         if (obj == nil) obj = new ExampleClass ();
         return obj;
    }

    public ArrayList<SearchModel> cache;
 }

2.In the from activity,

ExampleClass .instance().cache = Cus_Obje_arraylist;

3.Then in the to activity, you can get it from the bridge class.

ArrayList<SearchModel> Cus_Obje_arraylist = ExampleClass.instance().cache;
Shyam
  • 6,376
  • 1
  • 24
  • 38