0

How I can parse Arraylist of JSON from one Fragment to another fragment here is my arraylist code where I am getting arraylist from my model:

private void setListOffers(JSONArray categoryArray) {
    for (int i = 0; i < categoryArray.length(); i++) {
        try {
            JSONObject object = categoryArray.getJSONObject(i);
            hotDealID = object.getInt("ID");
            deals.add(new ListOffers(hotDealID));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

Here I am sending data from fraggment:

private ArrayList<ListOffers> deals = new ArrayList<>();
Fragment fragment = new HotDealFragment();
Bundle  bundle = new Bundle();
bundle.putParcelableArrayList("dealsId", deals);
fragment.setArguments(bundle);

When I put this code then deals gives exception that wrong second argument and ListOffers is my model where I am fetching data

and here is my model list Offers:

private int Id;

public ListOffers(int Id){
    this.Id = Id;
}

public void setId(int id) {
    Id = id;
}

public int getId() {
    return Id;
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
ansar abbas
  • 11
  • 1
  • 8

6 Answers6

1

One possible problem:

Your ListOffers model does not implement parcelable. Your model should implement parcelable. You can get help from this link

https://github.com/codepath/android_guides/wiki/Using-Parcelable

import android.os.Parcel;
import android.os.Parcelable;

public class ListOffers implements Parcelable {

private int Id;

public ListOffers(int Id) {
    this.Id = Id;
}

public void setId(int id) {
    Id = id;
}

public int getId() {
    return Id;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(Id);
}

protected ListOffers(Parcel in) {

    Id = in.readInt();
}

public static final Creator<ListOffers> CREATOR = new Creator<ListOffers>() {

    @Override
    public ListOffers createFromParcel(Parcel in) {

        return new ListOffers(in);
    }

    @Override
    public ListOffers[] newArray(int size) {

        return new ListOffers[size];
    }
};
}
Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30
1

Using hashmap you can pass

HashMap <String,ArrayList<ListOffers>> hashMap;
Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);

And get it using below code

Intent intent = getIntent();    
hasMap= intent.getSerializableExtra("hashMap");
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
Vishal Dobariya
  • 321
  • 1
  • 11
0

your datamodel ListOffers should be implement the Parcelable then only you can pass as ParcelableArrayList.

refer

Help passing an ArrayList of Objects to a new Activity

sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Using Parcelable your model will be

public class ListOffers implements Parcelable {

    private int Id;

    /**
     * Constructs a Model from a Parcel
     *
     * @param parcel Source Parcel
     */
    public ListOffers(Parcel parcel) {
        this.Id = parcel.readInt();
    }

    public ListOffers(int Id) {
        this.Id = Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public int getId() {
        return Id;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(Id);
    }

    // Method to recreate a Model from a Parcel
    public static Creator<ListOffers> CREATOR = new Creator<ListOffers>() {

        @Override
        public ListOffers createFromParcel(Parcel source) {
            return new ListOffers(source);
        }

        @Override
        public ListOffers[] newArray(int size) {
            return new ListOffers[size];
        }

    };
}

Sending ParcelableArrayList from one fragment to other -

private ArrayList<ListOffers> deals = new ArrayList<>();
setListOffers(categoryArray);
Fragment fragment = new HotDealFragment();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("dealsId", deals);
fragment.setArguments(bundle);

Fetching it into other Fragment in it's onCreate -

ArrayList<ListOffers> deals = new ArrayList<>();
if (getArguments() != null) {
    deals = getArguments().getParcelableArrayList("dealsId");
}

Hope this will help you :)

Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
0

Here is an easier approach.

Most times when one wants to send this data is because they want it to be accessed in the new class for just a short time. Instead of sending the data every time, go to the fragment that contains the data and make it public and static.

public static ArrayList<ListOffers> deals;

Access it in your desired activity

MainFragment.deals
Lucem
  • 2,912
  • 3
  • 20
  • 33
0

Make your ListOffer implements Serializable and then use

private ArrayList<ListOffers> deals = new ArrayList<>();
Fragment fragment = new HotDealFragment();
    Bundle  bundle = new Bundle();
    bundle.putSerializable("dealsId", deals);
    fragment.setArguments(bundle);

Here is how to get that

if (getArguments() != null)  {
       mdealsId = (ListOffers)getArguments().getSerializable("dealsId");
 }