1

I want to pass ArrayList of NameValuePair from one fragment to another, here is my code

    param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair("member_id",mem_id));
    param.add(new BasicNameValuePair("country_id",countryid+""));
     Bundle urlbundle = new Bundle();

    urlbundle.putParcelableArrayList("params",(ArrayList<? extends Parcelable>) param);         
    result=new SearchResults();
    result.setArguments(urlbundle);

I am trying to get data in this way

    Bundle urlbundle=this.getArguments();
    param=urlbundle.getParcelableArrayList("params");

But it is giving error Bound mismatch: The generic method getParcelableArrayList(String) of type Bundle is not applicable for the arguments (String). The inferred type NameValuePair is not a valid substitute for the bounded parameter

Can anyone help me to solve this or How to do this correctly?

n1m1
  • 869
  • 1
  • 9
  • 20
  • I would suggest, Create NameValuePair in Fragment. Just pass all values in a normal way like your are passing a string and In Fragment create the NameValuePair – Hardik Trivedi Sep 29 '14 at 09:14

4 Answers4

1

After reading all the answers here, finally I would like to share how I have solved this. ie,using Constructors

param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("member_id",mem_id));
param.add(new BasicNameValuePair("country_id",countryid+"")); 
result=new SearchResults(param);
Util.replaceFragment(result, (NavDrawer) getActivity());

Then in SearchResults,

ArrayList<NameValuePair> param;    
public SearchResults(ArrayList<NameValuePair> list) {
        this.param = list;
    }

Thanks for all your answers

n1m1
  • 869
  • 1
  • 9
  • 20
0

NameValuePair is neither Parcelable nor Serializable, so you can not pass the whole object through Fragment. What you can do is to put every single string in the bundle and build the ArrayList<NameValuePair> in the "receiver" Fragment

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • ... or use HashMap (Serializable) ... or use array of keys and arrays of values like ArrayList keys, ArrayList values ... – Selvin Sep 29 '14 at 09:15
  • there is no advantages of use an `HashMap` in this case, since the bundle is mapped on a HashMap. About the ArrayList, I would rather use a `Bean`, instead of two `ArrayList`s to map key and value in the same object, @Selvin. Still thanks for the suggestion/contribution – Blackbelt Sep 29 '14 at 09:18
0

NameValuePair not implement Parcelable interface. You should extends BasicValueName and implements Parcelable

Other option is communicate with Activity and hold in Activity the data, and retrieve it when the new fragmen is loaded.

RdlP
  • 1,366
  • 5
  • 25
  • 45
-1

First of all, fragments should never communicate between themselves. Instead they should communicate via a call back method implemented by the activity that hosts them. Again, you can not call setArguments(urlbundle); on a fragment instance for more than once in its life time.

So you should first create a call back interface for Fragment1 which the parent activity should implement. Your second fragement, say the ResultFragment, should have a method that accepts ArrayList as argument and take care of displaying it on the interface. Look at the accepted answer for this question to see how easily you can achieve it.

Communication between Fragments

You may also want to read Why direct communication between fragments is not recommended?

Alternatively, if your fragments are loaded in two different activities, your only choice may be to extend Application class or use a singleton class that can hold a static reference to ArrayList you want to share between activities.

Community
  • 1
  • 1
Vishnuprasad R
  • 1,682
  • 13
  • 23