-1

I am creating a pretty simple app that has a custom listItem (each listitem object contains a string, an integer, and a second integer value).

What I want to achieve is to allow the user to tap on the listitem, and have it open in a new screen. it will simply display the same information.

However I feel a bit lost as a newbie. How do I sent the listItem data from the first screen to the second? I am trying to achieve something like this tutorial - but I cannot figure out how they made it work: http://www.codelearn.org/android-tutorial/twitter/intent-example-tweet-detail-screen-module

I've looked on stackoverflow for the answer, but I guess I don't know what to search for because I haven't found one

user198923
  • 489
  • 1
  • 7
  • 19
  • use an Intent to start the second activity. Put the data from the string into the Intent, and extract it in the second activity – coolharsh55 Jul 05 '14 at 14:56
  • Can I send the whole object at once? I would prefer to understand how to send the entire object rather than sending one piece at a time – user198923 Jul 05 '14 at 15:00
  • yes, you can send entire objects. Check Itent.putExtras() and Bundle. If your Object contains only Primitve types, then its better to put them in the Intent itself and create a new object in your second activity. – coolharsh55 Jul 05 '14 at 15:03

1 Answers1

0

You could implement your class with Serializable.

import java.io.Serializable;

@SuppressWarnings("serial") //with this annotation we are going to hide compiler warning
public class CustomObject implements Serializable {

public CustomObject(double id, String name){
    this.id = id;
    this.name = name;
}

public double getId() {
    return id;
}
public void setId(double id) {
    this.id = id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}

private double id;
private String name;

}

We will send the object called customObject from X activity to Y activity. In your onItemClik

CustomObject customObject = new DCustomObjectneme(4,"YourName");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", customObject);
startActivity(i);

In Y activity we are getting the object.

Intent i = getIntent();
CustomObject customObject = (CustomObject)i.getSerializableExtra("sampleObject");

Try it.

Farouk Touzi
  • 3,451
  • 2
  • 19
  • 25