1

I am getting error on this code

listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);

this is how I pass intent

Intent intent = new Intent(mcontext,SingleObjectActivity.class);

intent.putParcelableArrayListExtra("Object_list", personArrayList);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);

this is second activity

public class SingleObjectActivity extends Activity
{
    public static final String OBJECT_LIST = "Object_list";
    private ArrayList<Listitem> Objects;
    public ImageView imgview;
    private Listitem listObjects;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singleobject);

        Bundle extras = getIntent().getExtras();

        imgview = (ImageView) findViewById(R.id.funnyimage);

        if (extras.containsKey(OBJECT_LIST)) {
            this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);
        } else {
            this.listObjects = null;
        }


        if (this.listObjects != null) {
            Picasso.
                    with(getApplicationContext()).
                    load(this.listObjects.getUrl())
                            //load()
                    .placeholder(R.drawable.logo)
                    .fit()
                    .noFade()
                    .into(imgview);

        }
    }
}

This is listitem

public class Listitem implements Parcelable {
    private int order;
    private String id;
    private String url;
    private String userName;
    private int likes;
    //String name;

   public Listitem(Parcel in){
        this.id = in.readString();
        this.url = in.readString();
       //   this.name = in.readString();

    }


    public Listitem(int order, String id, String url, String userName, int likes) {
        this.id = id;
        this.url = url;
       this.userName = userName;
       this.order = order;
       this.likes = likes;
    }

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

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }


    public int getLikes() {
        return likes;
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        // dest.writeString(this.name);
        dest.writeString(this.url);

    }

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

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
3
  • you are sending list but getting just 1 object Commented Sep 13, 2016 at 11:39
  • Why did you comment out the correct code for this case? Commented Sep 13, 2016 at 11:46
  • @MuratK. I want to get the result of listitem to put it in picasso. I want to have listobjects.geturl() Commented Sep 13, 2016 at 11:50

2 Answers 2

8

You passed full array list but getting just one object

for example try like this:

for sending

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

Intent i = new Intent(this,SecondActivity.class);

ArrayList<MyParcelable> testing = new ArrayList<MyParcelable>();

i.putParcelableArrayListExtra("extraextra", testing);
startActivity(i);
}

for receive

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<MyParcelable> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
}
}

// simple class that just has one member property as an example

 public class MyParcelable implements Parcelable {
private int mData;

/* everything below here is for implementing Parcelable */

// 99.9% of the time you can just ignore this
@Override
public int describeContents() {
    return 0;
}

// write your object's data to the passed-in Parcel
@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(mData);
}

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
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];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private MyParcelable(Parcel in) {
    mData = in.readInt();
}

}

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

9 Comments

okay.. then if want to get the value from the listitem.geturl() how to do that?
as in example you have "testing" arraylist and which item's you need please get by using testing.get(0).geturl()
yes put direcly in picsso, if you want to populate in list and then pass in adapter and in adapter you get item and pass. but if you want for specific position you need to give that position
If you need just single item then why you are passing full array list, just pass single object
|
2

You are putting an ArrayList

intent.putParcelableArrayListExtra("Object_list", personArrayList);

But you're trying to fetch just a single Parcelable:

this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);

Those are different types. If you are going to save an array list then you need to call getParcelableArrayListExtra

9 Comments

you mean like this listObjects = (Listitem) extras.getParcelableArrayListExtra(OBJECT_LIST); ? I get error
Ofc you have get an error. You're casting the list to ListItem. You should cast it to ArrayList<ListItem> because that's a list. Not a ListItem
I am being confused between the listitem and arraylist .. okay then I got the arraylist , how then I get lisitem ?
What are you trying to transfer? A list of ListItems? Or a single ListItem? The diferente between those is simple. One is a list (with more than one item) and the other is just one item. You are transferring a list but trying to get a single item. Tell me what you want to transfer
I am passing in an intent a list contain several info personArrayList.add(new Listitem(item.getOrder(), item.getId(), item.getUrl(), item.getUserName(), item.getLikes())); i want in another activity to use id, url and likes , for now i am using url in the picasso .. did you understand what i am doing ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.