Neha Tyagi
Neha Tyagi

Reputation: 3821

Sending arraylist of objects using serializable

I am storing my JSON data into a arraylist of objects, my object class is Venue and I am passing that data into another activity using serializable from fragment but I am not receiving any values in the actvity. It is receiving the extra. bundle is not null.

My code is:

Fragment:

 Intent intent = new Intent(getActivity(), SearchVenueActivity.class);

 //pass values
 Bundle bundle = new Bundle();
 bundle.putSerializable("arrayListVenue",arrayListVenue);
 intent.putExtras(bundle);
 startActivity(intent);

Activity:

if (getIntent().hasExtra("arrayListVenue")) {
    Bundle bundle = getIntent().getExtras();
    if(bundle!=null)
        rowItems = (ArrayList<Venue>) bundle.getSerializable("arrayListVenue");
    else
        Log.e("null","null");
}

Venue Class:

public class Venue implements Serializable{

    String venue_id;
    String venue_name;
    String venue_address;
    String venue_city;

    public String getVenue_city() {
        return venue_city;
    }

    public void setVenue_city(String venue_city) {
        this.venue_city = venue_city;
    }

    public Venue(String venue_id, String venue_name, String venue_address, String venue_city, String venue_zip, String venue_phone, String venue_mobile) {
        this.venue_id = venue_id;
        this.venue_name = venue_name;
        this.venue_address = venue_address;
        this.venue_city = venue_city;

        this.venue_zip = venue_zip;
        this.venue_phone = venue_phone;
        this.venue_mobile = venue_mobile;
    }

    public String getVenue_id() {
        return venue_id;
    }

    public void setVenue_id(String venue_id) {
        this.venue_id = venue_id;
    }

    public String getVenue_name() {
        return venue_name;
    }

    public void setVenue_name(String venue_name) {
        this.venue_name = venue_name;
    } 
}

Upvotes: 10

Views: 23256

Answers (4)

Surya
Surya

Reputation: 638

Use Array List instead of Bundle Elements. In your Main Activity, try this:

Intent intent = new Intent(this, newactivity.class);
intent.putParceableListArrayExtra("your_array_list_name", arrayList);

In the next activity where you want to retreive it, just use:

getIntent().getExtra("your_array_list_name", new ArrayList<> arrayList2);

Upvotes: 0

jeet parmar
jeet parmar

Reputation: 868

try this way may this help you

First you want to make

Class Venue implements Serializable

public class Venue implements Serializable {
  /**
  * 
  */

private static final long serialVersionUID = 1L;

 }

In your FirstActivity do this way

  ArrayList<Venue> VenueArrayList = new ArrayList<Venue>();

  Intent intent = new Intent(this,secondActivity.class);
  intent.putExtra("VenueArrayList", VenueArrayList);

In your SecondActivity do this way

  ArrayList<Venue> VenueArrayList;
  VenueArrayList = (ArrayList<Venue>) getIntent().getSerializableExtra(
        "VenueArrayList");

Upvotes: 18

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

Use intent.putParcelableArrayListExtra("arrayListVenue",arrayListVenue); for putting arraylist to intent and use intent.getParcelableArrayExtra("arrayListVenue") for get arraylist back from intent in your SearchVenueActivity activity
Edit Tutorial for using parcelable

Upvotes: 3

EE66
EE66

Reputation: 4651

Learn from my mistakes.

I've tried to send Arrays and big objects using Serializablethen my app got really slow. Did some tests and found out that when it took a lot of cpu and time to parse it. Therefore (altough its not what you would call best practice) i've created a CacheManager that store object for short time periods and used it to pass larger object between fragments activities.

If you want to make a more readical change to your design (later on in my project i did exactly that) then seperate the data from your fragment/activity completely and load it by LoaderManagers and pass only specific id's or stuff like that (stored stuff in the db in my case so it was easier).

Upvotes: 4

Related Questions