How can i pass array list from one activity to another activity using intent.
From activity
ArrayList<ServicesInfo> bookedService = new ArrayList<ServicesInfo>();`
Intent intent = new Intent(getActivity() , Proceedtocart.class);
intent.putExtra("Listview",bookedService);
startActivity(intent);
To activity
bookedService = (ArrayList<BookedInfo>) getIntent().getSerializableExtra("Listview");
while running am getting error as "java.lang.runtimeexception parcel unable to marshal value android"
Help to to fix this issue
Try this :
Intent intent = new Intent(this, NextActivity.class);
intent.putStringArrayListExtra("Listview", bookedService);
startActivity(intent);
and on NextActivity :
yourArrayList = getIntent().getStringArrayListExtra("Listview");
You can use
public class ContactInfo {
private String name;
private String surname;
private int idx;
// get and set methods
}
public class ContactInfo implements Parcelable {
private String name;
private String surname;
private int idx;
// get and set method
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(surname);
dest.writeInt(idx);
}
// Creator
public static final Parcelable.Creator CREATOR
= new Parcelable.Creator() {
public ContactInfo createFromParcel(Parcel in) {
return new ContactInfo(in);
}
public ContactInfo[] newArray(int size) {
return new ContactInfo[size];
}
};
// "De-parcel object
public ContactInfo(Parcel in) {
name = in.readString();
surname = in.readString();
idx = in.readInt();
}
}
Put
Intent i = new Intent(MainActivity.this, ActivityB.class);
// Contact Info
ContactInfo ci = createContact("Francesco", "Surviving with android", 1);
i.putExtra("contact", ci);
Get
Intent i = getIntent();
ContactInfo ci = i.getExtras().getParcelable("contact");
tv.setText(ci.toString()); // tv is a TextView instance
Related
I declare a List object in a method of a java class, which gets filled with data inside this method. Now I want to pass the filled list to another activity. How should I do that?
if you are in a JAVA class and want to call the list in an Activity, them simply put the method return type as list and call it in the required Activity . Demo code is as follows
class ReturnList{
public List method(){
List list=new ArrayList();
return list;}
}
class ActivityDemo extends Activity{
onCreate(){
///activity code
ReturnList returnListObj=new ReturnList();
List listData= returnListObj.method();
//deal with list here
}}
First make your object class parcelable.
Pass data as a bundle
Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", yourObject);
intent.putExtras(bundle);
startActivity(intent);
Retrieving the data:
Bundle bundle = getIntent().getExtras();
yourObject = bundle.getParcelable("data");
Hope it helps :)
Define your class as below
public class CategoryEntity implements Parcelable{
public int id;
public String name;
public String imageOriginal;
public String imageThumb;
public String description;
public String status;
public String createdAt;
public String updatedAt;
public int imageDummy;
protected CategoryEntity(Parcel in) {
id = in.readInt();
name = in.readString();
imageOriginal = in.readString();
imageThumb = in.readString();
description = in.readString();
status = in.readString();
createdAt = in.readString();
updatedAt = in.readString();
imageDummy = in.readInt();
}
public static final Creator<CategoryEntity> CREATOR = new Creator<CategoryEntity>() {
#Override
public CategoryEntity createFromParcel(Parcel in) {
return new CategoryEntity(in);
}
#Override
public CategoryEntity[] newArray(int size) {
return new CategoryEntity[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeString(imageOriginal);
dest.writeString(imageThumb);
dest.writeString(description);
dest.writeString(status);
dest.writeString(createdAt);
dest.writeString(updatedAt);
dest.writeInt(imageDummy);
}
}
Pass your data using Bundle
startActivity(new Intent(CategoriesListingActivity.this, StoryDetailActivity.class)
.putExtra("List", your list with model class));
Handle your list in another activity
Bundle extra = getIntent().getExtras();
if (extra != null) {
storyList = (ArrayList<CategoryEntity>) extra.getParcelable("StoryListing");
}
Hope this will helps you.
Pass your list in intent.putExtra(), for example
Intent intent = new Intent(getApplicationContext() , YourNextActivity.class);
intent.putExtra("list" , (Serializable) yourList);
startActivity(intent);
and for retrieve list in NextActivity
Intent intent = getIntent();
intent.getSerializableExtra("list");
I am trying to pass a custom object from one activity to another one.
I found that we can use a bundle to pass data into the intent and that we need a parcelable interface implemented in our class.
In the following code I removed the useless stuff.
public class TravelCard implements Parcelable {
public static final Creator<TravelCard> CREATOR = new Creator<TravelCard>() {
#Override
public TravelCard createFromParcel(Parcel in) {
return new TravelCard(in);
}
#Override
public TravelCard[] newArray(int size) {
return new TravelCard[size];
}
};
private String travelTitle, travelCountry, dateRange, numOfPerson;
private List<TravelDay> days;
protected TravelCard(Parcel in) {
travelTitle = in.readString();
travelCountry = in.readString();
dateRange = in.readString();
numOfPerson = in.readString();
in.readTypedList(days, TravelDay.CREATOR);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.travelTitle);
dest.writeString(this.travelCountry);
dest.writeString(this.dateRange);
dest.writeString(this.numOfPerson);
dest.writeTypedList(this.days);
}
In this class I have a List of another custom object:
public class TravelDay implements Parcelable {
public static final Creator<TravelDay> CREATOR = new Creator<TravelDay>() {
#Override
public TravelDay createFromParcel(Parcel in) {
return new TravelDay(in);
}
#Override
public TravelDay[] newArray(int size) {
return new TravelDay[size];
}
};
public int current_day;
private String title, note, dateOfToday;
protected TravelDay(Parcel in) {
title = in.readString();
note = in.readString();
dateOfToday = in.readString();
current_day = in.readInt();
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(note);
dest.writeString(dateOfToday);
dest.writeInt(current_day);
}
I think until here everything should be fine.
In my activity :
Intent intent = new Intent(MainActivity.this, TravelDayActivity.class);
Bundle data = new Bundle();
data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
intent.putExtras(data);
startActivity(intent);
And then in the TravelDayActivity :
Intent intent = getIntent();
Bundle data = intent.getExtras();
TravelCard travelCard = data.getParcelable(DataKey.TRAVEL_CARD_BUNDLE);
Every time I tried to access the TravelDayActivity the app stop running.
I used the debug mode to search something wrong but I did not find anything.
Thank you in advance
EDIT :
In the MainActivity that's how I get the item variable:
mAdapter = new TravelCardAdapter(travelCards, new TravelCardAdapter.OnItemClickListener() {
#Override
public void onItemClick(TravelCard item) {
Intent intent = new Intent(MainActivity.this,TravelDayActivity.class);
Bundle data = new Bundle();
data.putParcelable(DataKey.TRAVEL_CARD_BUNDLE,item);
intent.putExtras(data);
startActivity(intent);
}
});
Whenever I click on one of the recycler view item this code above is executed.
After the suggestion of #BVantur, I solved my problem using the parceler library :
parceler library
It has been very helpful, easy to use and a good documentation! I highly recommend it.
I tried creating a public class to try transferring data from one activity to another. But when I tried setting the information of this class altogether I manage to get the Int variables but not the String and when I tried to get this data it was blank.
This is My MainActivity
public void toyota_yaris(View view) {
CurrentCar currentcar = new CurrentCar();
currentcar.setInfo("Toyota Yaris",130,8,1160,7);
Intent switchScreen = new Intent(MainActivity.this,CarActivity.class);
MainActivity.this.startActivity(switchScreen);
}
This is My CarActivity
CurrentCar currentcar = new CurrentCar();
TextView name = (TextView) findViewById(R.id.name);
name.setText(currentcar.getName());
TextView speed = (TextView) findViewById(R.id.speed);
speed.setText(String.valueOf(currentcar.getSpeed()));
This is My CurrentCar Class (getter and setter class)
public class CurrentCar {
private String mName;
private int mSpeed;
private int mAge;
private int mMileage;
private int mSeats;
public void setInfo(String Name,int Speed,int Age,int Mileage,int Seats ) {
mName = Name;
mSpeed = Speed;
mAge = Age;
mMileage = Mileage;
mSeats = Seats;
}
public String getName() {
return mName;
}
public int getSpeed() {
return mSpeed;
}
public int getAge() {
return mAge;
}
public int getMileage() {
return mMileage;
}
public int getSeats() {
return mSeats;
}
}
If you want to pass data from one activity to another , then attach it with intents.
Example-
In MainActivity-
Bundle bundle= new Bundle();
bundle.putString("name", "A");
bundle.putString("speed", "100");
Intent intent= new Intent(MainActivity.this,CarActivity.class);
intent.putExtras(bundle);
startActivity(intent);
In carActivity-
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
String speed=bundle.getString("speed");
and then set these values in text views.
in My CarActivity you are creating new Object : CurrentCar currentcar = new CurrentCar();
so if you call name.setText(currentcar.getName()); then it will simply return null because strings default is null.
just use the object being created in My MainActivity
I am trying to pass an ArrayList of objects MyItem from one activity to another. From what i understand i need to implement Parcable in MyItem class. So this is what i'va done so far:
public class MyItem implements ClusterItem, Parcelable {
private LatLng mPosition=null;
private String aString;
public MyItem(double lat, double lng, String aString) {
mPosition = new LatLng(lat, lng);
this.aString= aString;
}
#Override
public LatLng getPosition() {
return mPosition;
}
public String getString(){
return aString;
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
//dest.writeLngLat ?
dest.writeString(postID);
}
public static final Parcelable.Creator<MyItem> CREATOR
= new Parcelable.Creator<MyItem>() {
public MyItem createFromParcel(Parcel in) {
return new MyItem(in);
}
public MyItem[] newArray(int size) {
return new MyItem[size];
}
};
private MyItem(Parcel in) {
//mPosition = in.readLngLat ?
aString = in.readString();
}
}
First question: How could i write LngLat field in writeToParcel and how could i decleare it in MyItem(Parcel in) constructor?
Second question: Is this going to be enough so this piece of code could work?
ArrayList<MyItem> listItems = new ArrayList<>();
Iterator<MyItem> items = cluster.getItems().iterator();
while (items.hasNext()) {
listItems.add(items.next());
}
Intent intent = new Intent(MapsActivity.this, ClusterPosts.class);
intent.putParcelableArrayListExtra("oO", listItems);
startActivity(intent);
and then in CluserPosts:
Intent intent = getIntent();
ArrayList<MyItem> post = intent.getParcelableArrayListExtra("oO");
for(MyItem item : post){
Log.d("elaela", item.getString());
}
Write parcel as -
dest.writeDouble(mPosition.latitude);
dest.writeDouble(mPosition.longitude);
You can write latitude and longitude as doubles only to the destination and then form a position.
dest.writeDouble(lat);
dest.writeDouble(long);
I'm having troubles with getting content from getParcelableArrayList.
I have data model class that extends parcelable
#DatabaseTable(tableName = "note")
public class Log implements Parcelable {
#DatabaseField(id = true, index = true)
UUID id;
#DatabaseField
String title;
#DatabaseField
String description;
public Log() {
}
#Override
public int describeContents() {
return 0;
}
public Log(Parcel in) {
this.title = in.readString();
this.description = in.readString();
}
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(title);
out.writeString(description);
}
public void readFromParcel(Parcel in){
title = in.readString();
description = in.readString();
}
public static final Parcelable.Creator<Log> CREATOR = new Parcelable.Creator<Log>(){
public Log createFromParcel(Parcel in){
return new FoodLog(in);
}
public Log[] newArray(int size){
return new FoodLog[size];
}
};
And I want make feature for editing entry in database.
I'm sending data from activity one to activity two via bundle like this
Activity one:
Intent intent = new Intent(LogList.this, AddLogActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("list", new ArrayList<>(mLog));
intent.putExtras(bundle);
startActivity(intent);
Receiving bundle via intent in Activity two:
Intent mIntent = getIntent();
if (mIntent != null) {
Bundle bundle = mIntent.getExtras();
if (bundle != null) {
mLogParcel = bundle.getParcelableArrayList("list");
}
}
So, my question is how to get data from passed arraylist in Activity two?
I have data saved inArrayList<Log> mLogParcel;, and I tried using readFromParcel on mLogParcel, but without positive results.
How to get data based on data model in this case?
Thanks a lot!