This question already has answers here:
Passing custom objects between activities?
(5 answers)
Closed 5 years ago.
I want to send user define object from one activity to another in android application.
I have created user class object and send this user object to my second activity from first activity.
Implement your class with Serializable interface.
Then pass the object using
intent.putExtra("MyClass", obj);
and retrieve object by calling
getIntent().getSerializableExtra("MyClass");
See this post
Make sure your User class implements Parcelable.
public class User implements Parcelable {
...........
...............
}
Send User object to SecondActivity as below:
User userObject = new User();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("user_data", userObject);
startActivity(intent);
Retrieving the User object in SecondActivity.
User user = (User) getIntent().getParcelableExtra("user_data");
Here is good Tutorial about using Parcelable.
Hope this will help~
Iplemented my user define class to Parcelable interface.
Related
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 3 years ago.
I am reading User class from database with this code :
mDatabase.orderByChild("name")
.equalTo(s1)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
User user = userSnapshot.getValue(User.class);
}
}
I want to display content of User object in a new activity. How do I pass User object to new activity?
Using Intent, you can share data between two activities.
In your case, you have to make user object parcelable.
Implement Parcelable interface in your user class.
Send the user object in intent like
intent.putExtra("user", userObject)
In your second activity get user object from intent :
User user = (User) getIntent().getExtras().getParcelable("user")
You Can Transfer Data Using Following Way:
Passing Data Through Intent(based on Key value or In Bundle Format)
Using Interface
Creating a Method in Another Activity and Call This method From First Activity (Using Public Method)
I am trying to send a List<Question> to other activity but I am receiving the error "java.util.ArrayList cannot be cast to android.os.Parcelable".
FirstActivity
List<Question> list;
list = response.body().items;
Intent myIntent = new Intent(SearchActivity.this, RestaurantActivity.class);
myIntent.putExtra("restaurants", (Parcelable) list);
SearchActivity.this.startActivity(myIntent);
SecondActivity
Intent intent = getIntent();
List<Question> restaurants = intent.getExtras().getParcelable("restaurants");
textView = (TextView)findViewById(R.id.textView);
textView.setText(restaurants.get(0).title);
What is the problem?
The reason it doesn't work is that ArrayList itself does not implement the Parcelable interface. However, some Android classes like Intent and Bundle have been set up to handle ArrayLists provided that the instances they contain are of a class which does implement Parcelable.
So, instead of putExtra, try using the putParcelableArrayListExtra method instead.
You'll need to use get getParcelableArrayListExtra on the other side.
Be aware that this only works with ArrayLists, and the Question class will need to implement Parcelable.
I am trying to send this object information to another activity with intent. All information is in this picture.
First Activity
Intent intent = new Intent(getApplicationContext(),AnotherActivity.class);
intent.putSerializable("VALUE", listItem);
Second Activity
Intent intent=this.getIntent();
Object list = intent.getSerializable("VALUE");
Please change Object to GeoLabStudent
you should use adapter's reference to get item
GeoLabStudent object = (GeoLabStudent)geoLabAdapter.getItem(position);
Make your model GeoLabStudent to serializable or parcelable for sending your object to other activity
Example
This question already has answers here:
Passing ArrayList through Intent
(6 answers)
Closed 9 years ago.
Is there a way to pass an ArrayList <ArrayList<Integer>> floors to another activity through Bundle?
Thanks
Is there a way to pass an ArrayList > floors to
another activity through Bundle?
Unfortunetly not.
If you would have ArrayList without nested it will work with putIntegerArrayList(key, value) and getIntegerArrayList(key).
But there is for sure another approach(es).I will explain you one possible way.
You can create class that will implement Serializable interface and in this class just create field and appropriate getter. I will give you basic example. Then you will pass Serializable through Activities.
public class DataHelper implements Serializable {
private ArrayList<ArrayList<Integer>> floors;
public DataHelper(ArrayList<ArrayList<Integer>> floors) {
this.floors = floors;
}
public ArrayList<ArrayList<Integer>> getList() {
return this.floors;
}
}
Save it to Bundle:
Bundle b = new Bundle();
b.putSerializable("floors", new DataHelper(floors));
and retrieve in target Activity:
getIntent().getExtras().getSerializable("floors");
To pass the arraylist from first activity to second activity.
Intent intent = new Intent(context, SecondActity.class);
intent.putIntegerArrayListExtra("arraylist",integerList); //integerList is ArrayList<Integer>
startActivity(intent);
To get the arrayList in second Activity.
ArrayList arrayList<Integer> = getIntent().getIntegerArrayListExtra("arraylist")
Read here.
If you want to pass the custom object between activities then read this thread.
This is on the android platform, i have a list of objects of type (FitItem), and i want pass the list from my activity A to another activity B, and on the activity B i want get the list of objects again
Intent yourIntent = new Intent(activityA.this, activityB.class);
Bundle yourBundle = new Bundle();
yourBundle.putString("name", value);
yourIntent.putExtras(yourBundle);
startActivity(yourIntent);
And you get the value in the next Activity (in your onCreate()):
Bundle yourBundle = getIntent().getExtras();
String s = yourBundle.getString("name");
This example is passing a String, but you should be able to grasp how to use it for other objects.
For custom classes:
You will have to have your FitItem class implements Parcelable.
Then in Activity A, from an Intent object, use putParcelableArrayListExtra to pass the list of FitItem to Activity B and in your Activity B, use getParcelableArrayListExtra to retrieve the list of FitItem
If you want to pass list of String, Integer, Float ..., refer to bschultz post
You must serialize your object.
"Seriawhat?"
Ok, first things first: What is object serialization?
"Got it, but how do I do that?"
You can use Parcelable (more code, but faster) or Serializable (less code, but slower). Parcelable vs Serializable.
"Save me some time, show me the code!"
Ok, if you'll use Parcelable, see this.
And if you'll use Serializable, see this and/or this.
Hope that helps!
If they're if the object is serializable, just add them as extras to the intent. I think something like this:
// in Activity A, when starting Activity B
Intent i = new Intent(this, ActivityB.class);
i.putExra("fitItemList", fitItemList);
startActivity(i);
// in Activity B (onCreate)
ArrayList<FitItem> fitItemList = savedInstanceState.getSerializableExtra("fitItemList");
edit:
Just like bschultz already posted :)
Implements Serializable in model class. Then you can pass model class using bundle/intent.