Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    } 
}
like image 695
Neha Tyagi Avatar asked Jul 10 '15 08:07

Neha Tyagi


3 Answers

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");
like image 91
jeet parmar Avatar answered Oct 18 '22 15:10

jeet parmar


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).

like image 43
EE66 Avatar answered Oct 18 '22 16:10

EE66


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

like image 26
Jaiprakash Soni Avatar answered Oct 18 '22 16:10

Jaiprakash Soni