N
N
newdancer2016-04-03 18:27:45
Java
newdancer, 2016-04-03 18:27:45

How to pass an arraylist to a fragment?

How to pass an arraylist to a fragment?
In MainActivity I create and partially fill

ArrayList<Wishbone> wishbones = new ArrayList<Wishbone>();

then I pass it to the adapter
mWishbonePagerAdapter = new WishbonePagerAdapter(getSupportFragmentManager(), this, wishbones);

Adapter inherited from FragmentPagerAdapter
Fragment is filling data. Each item has 2 pictures on which animation is drawn through the canvas when the user clicks on one of the two pictures. For animation, I need the imageview itself, for this I want to write it down by changing the arraylist. How to do it?
ArrayList is structured by class like this:
public class Wishbone
{
  public int getImgLeft() {
    return imgLeft;
  }

  public void setImgLeft(int imgLeft) {
    this.imgLeft = imgLeft;
  }

  public int getImgRight() {
    return imgRight;
  }

  public void setImgRight(int imgRight) {
    this.imgRight = imgRight;
  }

  public int getVubRez() {
    return vubRez;
  }

  public void setVubRez(int vubRez) {
    this.vubRez = vubRez;
  }

  public int getLeftRez() {
    return leftRez;
  }

  public void setLeftRez(int leftRez) {
    this.leftRez = leftRez;
  }

  public int getRightRez() {
    return rightRez;
  }

  public void setRightRez(int rightRez) {
    this.rightRez = rightRez;
  }

  public int getLeftVote() {
    return leftVote;
  }

  public void setLeftVote(int leftVote) {
    this.leftVote = leftVote;
  }

  public int getRightVote() {
    return rightVote;
  }

  public void setRightVote(int rightVote) {
    this.rightVote = rightVote;
  }

  public ImageView getImageLeft() {
    return imageLeft;
  }

  public void setImageLeft(ImageView imageLeft) {
    this.imageLeft = imageLeft;
  }

  public ImageView getImageRight() {
    return imageRight;
  }

  public void setImageRight(ImageView imageRight) {
    this.imageRight = imageRight;
  }
  int imgLeft;
  int imgRight;
  int vubRez;
  int leftRez;
  int rightRez;
  int leftVote;
  int rightVote;
  ImageView imageLeft;
  ImageView imageRight;

  Wishbone(int _imgLeft, int _imgRight, int _vubRez, int _leftRez, int _rightRez, int _leftVote, int _rightVote, ImageView _imageLeft, ImageView _imageRight) {
    imgLeft = _imgLeft;
    imgRight = _imgRight;
    vubRez = _vubRez;
    leftRez = _leftRez;
    rightRez = _rightRez;
    leftVote = _leftVote;
    rightVote = _rightVote;
    imageLeft = _imageLeft;
    imageRight = _imageRight;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Vasilenko, 2016-06-04
@SanchelliosProg

Well, first of all, the Wishbone class must implement the Parcelable interface . AndroidStudio should be easy to help with this, well, plus dig a little into the documentation on how to use this interface. Otherwise, you won't be able to push this ArrayList into an Intent, in this case.
Further, everything is simple. For example, you create an ArrayList collection, pass it to the Intent as an Extra (putExtra() method).
Parse the Intent in the target Activity/Fragment and pull out your collection using the getParcelableArrayList("KEY_WHICH_YOU_SPECIFIED_WHEN_PUT_THE_COLLECTION_IN_EXTRA")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question