2

I know how to send simple array via Bundle but know I need to send something like this:

 ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

How to send and retrive it via Bundle?

2
  • Sorry, what is a bundle? Commented Jul 6, 2016 at 12:14
  • 1
    You should prefer interface types (e.g. List, Map) to implementation types; you should encapsulate these in Objects other than data structures. Bad encapsulation. Commented Jul 6, 2016 at 12:14

6 Answers 6

2

This is bad encapsulation, poor object oriented thinking.

It sounds like you need a Song abstraction so you can return a List of Song instances:

List<Song> songList = new ArrayList<Song>();

I would avoid Java serialization; serialize them as JSON using Jackson.

Sign up to request clarification or add additional context in comments.

3 Comments

I need to retrive file name and file path, so my thinking is ok. It have to be two-dimensional
No, it's not okay. Those should be inside the Song abstraction. File path on what machine? More bad thinking. A URL would be more RESTful.
local machine, exactly SDcard
2

ArrayLists and HashMaps are serializable objects. So just use Bundle#putSerializable(bundleKey, serializable).

If you want a more OOP approach, you can encapsulate the data to be sent into an object (say of type SongData) that implements Parcelable. See this post for an example. Also see the reference documentation for Parcelable:

public class MyParcelable implements Parcelable {
  private int mData;

  public int describeContents() {
      return 0;
  }

  public void writeToParcel(Parcel out, int flags) {
      out.writeInt(mData);
  }

  public static final Parcelable.Creator<MyParcelable> CREATOR
         = new Parcelable.Creator<MyParcelable>() {
      public MyParcelable createFromParcel(Parcel in) {
          return new MyParcelable(in);
      }

      public MyParcelable[] newArray(int size) {
          return new MyParcelable[size];
      }
  };

  private MyParcelable(Parcel in) {
     mData = in.readInt();
  }
}

Comments

1

In the sender class

bundle.putSerializable("something", songsListData);

and in the receiver class

ArrayList<HashMap<String, String>> songsListData = (ArrayList<HashMap<String, String>>)bundle.getSerializable("something");
;

Comments

1

Use something like the following in the sender

ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
intent.putExtra("song_list", songsListData);

and than in the receiver class

ArrayList<String> myList = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("song_list");  

Comments

0
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
Gson gson = new Gson();
String str_json = gson.toJson(songsListData);
Bundle bundle=new Bundle();
bundle.putString("data",str_json);

Use Gson for converting arraylist data into string

Comments

0
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {}.getType();
ArrayList<HashMap<String, String>> arrayList = gson.fromJson(data, type);

For retrieving data in reciever class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.