简体   繁体   中英

Passing ArrayList<Object> in Android Studio w/ Lastfm API

I am trying to pass an ArrayList of type Album to another activity in Android Studio.

The problem is that the Lastfm API I am using does not implement Parcelable.

I tried making my own Album class and extending their Album class but I got the error

"there is no default constructor available in 'de.umass.lastfm.Album'

Quiz.java - intent.getParcel... not working as Album is not Parcelable

public class Quiz extends AppCompatActivity  {

private static TextView tv_quiz;
private static EditText et_quiz;
private ArrayList<Album> albums;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    Intent intent = getIntent();
    albums = intent.getParcelableArrayListExtra(MainActivity.ALBUMS_LIST);
}
}

The calling portion of my MainActivity.java.

Intent intent = new Intent(this, Quiz.class);
    intent.putExtra(ALBUMS_LIST, allAlbums);
    intent.putExtra(DIFFICULTY, difficulty);
    startActivity(intent);

Is there any way I can get around this?

Thanks anyway

Here is the solution open activity using this code

Bundle bundle = new Bundle();
bundle.putSerializable("data", allAlbums);
bundle.putString(DIFFICULTY", difficulty);
Intent intent = new Intent(this, Quiz.class);
    intent.putExtra(bundle);
    startActivity(intent);

get intent data

albums = (ArrayList<Album>) getIntent().getExtras().getSerializable("data");

updated Album Class

public class Album implements java.io.Serializable {

//your code
}

I took a look at the api you linked, and Album looks extremely difficult to parcel. I would say that you might be better off simply re-loading the list in the next Activity (rather than trying to pass the list).

The only way to "construct" a new Album instance is through the getInfo() static factory method. You could create a new class AlbumWrapper that is parcelable, send a list of AlbumWrapper through the Intent, and then use getInfo() on the other side to re-fetch the albums.

public class AlbumWrapper implements Parcelable {

    // insert CREATOR here

    public final String artist;
    public final String mbid;

    public AlbumWrapper(Album a) {
        this.artist = a.getArtist();
        this.mbid = a.getMbid();
    }

    private AlbumWrapper(Parcel in) {
        this.artist = in.readString();
        this.mbid = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(artist);
        dest.writeString(mbid);
    }

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

You can put your list of Albums in the intent like this:

ArrayList<AlbumWrapper> wrappers = new ArrayList<>();

for (Album album : albums) {
    AlbumWrapper wrapper = new AlbumWrapper(album);
    wrappers.add(wrapper);
}

intent.putParcelableArrayListExtra("ALBUM_WRAPPERS", wrappers);

And then in your next activity you can do something like:

List<AlbumWrapper> wrappers = getIntent().getParcelableArrayListExtra("ALBUM_WRAPPERS");
List<Album> albums = new ArrayList<>();

for (AlbumWrapper wrapper : wrappers) {
    Album album = Album.getInfo(wrapper.artist, wrapper.mbid, YOUR_API_KEY);
    albums.add(album);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM