-1

In my application whenever we click the "SEARCH" button it makes Http call and get the json as response. I parsed the json and save the data in arraylist bdata. ArrayList bdata = new ArrayList(); BusData contain getters and setters . I also used one adapter class for custom listview. now i want to set the adapter to listview but the listview is in another class (not in the class where search button is locate) so how to send my arraylist from one activity to another activity with the help of intents for set the adapter to listview.

Thank you.

  • you need to click SEARCH button on google.com – Selvin Dec 10 '14 at 14:05
  • The simplest would probably be to use EventBus to avoid having to serialize bdata. – cYrixmorten Dec 10 '14 at 14:07
  • A have asked a related question and had it debugged, see http://stackoverflow.com/q/27204324/3879470 also see see this http://stackoverflow.com/q/15747727/3879470 for detailed implementation. Its a simple implementation compared to others. – Mohammed Ali Dec 10 '14 at 14:30

3 Answers3

1

A way to do this is by serializing your arraylist to a JSON. Use GSON library

  compile 'com.google.code.gson:gson:2.3.1'

Add the above line in your build.gradle file.

Make a class to help you serializing your objects

public class SerializationHelper {
private static Gson gson = new Gson();

public static String serialize(Object object) {
    return gson.toJson(object);
}

public static Object deserialize(String json, Class classType) {
    return gson.fromJson(json, classType);
}

}

Now when you want to start the new activity add the serialized string.

String json=SerializationHelper.serialize(myArrayList);
intent.putExtra("data",json);

And in your new activity on create get it and create your object again.

String json=intent.getStringExtra("data");
Object deserializedObject=SerializationHelper.deserialize(json,ArrayList.class);

Now cast your object!

ArrayList<MyClass> myCoolArray=(ArrayList<MyClass>)deserializedObject.

Other simpler way is to make your arraylist static and public, and store it in an other class.

   public class GlobalStuff{
public static ArrayList<MyClass> myAwesomeList;
}

Now acces your list by GlobalStuff.myAwesomeList.

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
0

You need to create custom class (ArrayList<data class>) to Parcelable and then you can pass it to other activity.

http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
0

When passing data through activities you should use Parcelable, the are many examples in stackoverflow, this one is good:

https://stackoverflow.com/a/22446641/2091315

Next, you need to configure the intent, on the "sender" Activity:

Intent intent = new Intent(getApplicationContext(),
                            ReceiverActivity.class);

intent.putExtra("arrayListIdentifier",parcelableArrayClass);

startActivity(intent);

On the "receiver" Activity:

parcelableArrayClass myParcelableObject = (parcelableArrayClass) getIntent().getParcelableExtra("arrayListIdentifier");
Community
  • 1
  • 1
Fantini
  • 2,067
  • 21
  • 32