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);
Related
I got a JSON with an array of doubles and another for Strings. I am trying to create a Parcelable model class with the relevant methods. When the methods are created automaticaly, no line for the doubles' array list is created.
I looked for relevant questions and information, but, surprisingly, couldn't find any. I also added a jar file that was supposed to assist: android-parcelable-intellij-plugin.jar. Don't know what it's supposed to do and couldn't find information on how to use it.
My question is what should I write in the methods in order to get the array of doubles in a List.
The code:
public class Country implements Parcelable {
...
private List<String> timezones;
private List<Double> latlng;
protected Country(Parcel in) {
timezones = in.createStringArrayList();
this.latlng = new ArrayList<>(); // from jsonschema2pojo.org
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
//nothing for the double array
}
public List<Double> getLatlng() {
return latlng;
}
public void setLatlng(List<Double> latlng) {
this.latlng = latlng;
}
public List<String> getTimezones() {
return timezones;
}
public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
}
...
Thanks.
Try this below, it works for me :
public class Country implements Parcelable {
private List<String> timezones;
private List<Double> latlng;
public Country(List<String> timezones, List<Double> latlng) {
this.timezones = timezones;
this.latlng = latlng;
}
protected Country(Parcel in) {
timezones = in.createStringArrayList();
double[] doubleArray = in.createDoubleArray();
latlng = new ArrayList<>();
if (doubleArray != null) {
for (double ele : doubleArray) {
latlng.add(ele);
}
}
}
public static final Creator<Country> CREATOR = new Creator<Country>() {
#Override
public Country createFromParcel(Parcel in) {
return new Country(in);
}
#Override
public Country[] newArray(int size) {
return new Country[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(timezones);
double[] doubleArray = new double[latlng == null ? 0 : latlng.size()];
for (int i=0, len=latlng.size(); i<len; i++) {
doubleArray[i] = latlng.get(i);
}
dest.writeDoubleArray(doubleArray);
}
#Override
public String toString() {
return "Country{" +
"timezones=" + timezones +
", latlng=" + latlng +
'}';
}
}
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 know it's a commonly asked question and I looked at the solutions online, but I am having a difficulty implementing this on my own.
I have a class which contains three variables: Location, Date and Uri.
I have an Arraylist which consists of multiple Arraylists that contains said class.
For instance:
ArrayList<ArrayList<class>>
I am trying to pass this onto another activity, yet unsuccessful.
I tried both Parcelable and Serializable but none worked.
Edit: Source code added.
Class documentation:
public class imageHolder implements Parcelable
{
private Uri uri;
private Date date;
private Location loc;
public imageHolder(Uri uriAdd, Date dateAdded,Location imgLoc)
{
this.uri = uriAdd;
this.date = dateAdded;
this.loc = imgLoc;
}
public static final Creator<imageHolder> CREATOR = new Creator<imageHolder>() {
#Override
public imageHolder createFromParcel(Parcel in) {
return new imageHolder(in);
}
#Override
public imageHolder[] newArray(int size) {
return new imageHolder[size];
}
};
public Uri getURI() { return this.uri; }
public Date getDate() {return this.date; }
public Location getLocation() {return this.loc; };
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeSerializable(date);
parcel.writeParcelable(uri,i);
parcel.writeParcelable(loc, i);
}
protected imageHolder(Parcel in) {
date = (java.util.Date) in.readSerializable();
uri = in.readParcelable(Uri.class.getClassLoader());
loc = in.readParcelable(Location.class.getClassLoader());
}
}
First activity:
ArrayList<ArrayList<imageHolder>> sepImages = new ArrayList<ArrayList<imageHolder>>();
sepImages = groupPics(images);
Intent nextActivity = new Intent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images",images);
startActivity(nextActivity);
finishActivity(0);
Second activity:
ArrayList<ArrayList<imageHolder>> sepImages =
(ArrayList<ArrayList<imageHolder>>) getIntent().getParcelableExtra("images");
Log.d("stories","test");
I think the best thing to do here would be to create a new class that implements Parcelable and pass instances of this new class between activities.
public class ParcelableListOfLists implements Parcelable {
private ArrayList<ArrayList<imageHolder>> listOfLists;
public ParcelableListOfLists(ArrayList<ArrayList<imageHolder>> listOfLists) {
this.listOfLists = listOfLists;
}
public ArrayList<ArrayList<imageHolder>> getListOfLists() {
return listOfLists;
}
// parcelable implementation here
}
Once you have this class, you can be in full control of how your data is parceled, and that lets you do some things that you won't be able to do with the Android built-in offerings.
Here's one way you could parcel a list of lists:
#Override
public void writeToParcel(Parcel dest, int flags) {
if (listOfLists != null) {
dest.writeInt(listOfLists.size());
for (ArrayList<imageHolder> list : listOfLists) {
dest.writeTypedList(list);
}
} else {
dest.writeInt(-1);
}
}
And on the other side, you can recreate the list of lists like this:
public ParcelableListOfLists(Parcel in) {
int size = in.readInt();
if (size != -1) {
this.listOfLists = new ArrayList<>(size);
for (int i = 0; i < size; ++i) {
ArrayList<imageHolder> list = in.createTypedArrayList(imageHolder.CREATOR);
listOfLists.add(list);
}
} else {
this.listOfLists = null;
}
}
With all of this together, you can pass your list of lists between activities like this:
Intent nextActivity = new Intent(loadImages.this, storiesScreen.class);
nextActivity.putExtra("images", new ParcelableListOfLists(images));
startActivity(nextActivity);
and retrieve them in the next activity like this:
ParcelableListOfLists plol = getIntent().getParcelableExtra("images");
ArrayList<ArrayList<imageHolder>> images = plol.getListOfLists();
I think that you can use Gson too.
In your build.gradle
implementation 'com.google.code.gson:gson:2.8.4'
In the first activity
Gson gson = new Gson();
String json = gson.toJson(yourObject);
intent.putExtra("yourKey", json);
In the second activity
Gson gson = new Gson();
YourObject yourObject = gson.fromJson(getIntent().getStringExtra("yourKey"), YourObject.class);
Easy and fast.
Ok.
Your Model
public class ImageHolder implements Serializable {
// Use String here, to avoid serialization problems
private String uri;
private Date date;
private Location loc;
public ImageHolder() {
}
public ImageHolder(String uri, Date date, Location loc) {
this.uri = uri;
this.date = date;
this.loc = loc;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Location getLoc() {
return loc;
}
public void setLoc(Location loc) {
this.loc = loc;
}
#Override
public String toString() {
return "ImageHolder{" +
"uri=" + uri +
", date=" + date +
", loc=" + loc +
'}';
}
}
Create a Container class
public class Container implements Serializable {
private ArrayList<ImageHolder> list;
public Container() {
}
public Container(ArrayList<ImageHolder> list) {
this.list = list;
}
public ArrayList<ImageHolder> getList() {
return list;
}
public void setList(ArrayList<ImageHolder> list) {
this.list = list;
}
#Override
public String toString() {
return "Container{" +
"list=" + list +
'}';
}
}
Your First Activity
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
ImageHolder imageHolder0 = new ImageHolder("http://www.stackoverflow.com", new Date(), new Location("test0"));
ImageHolder imageHolder1 = new ImageHolder("http://www.google.com", new Date(), new Location("test1"));
ArrayList<ImageHolder> list = new ArrayList<>();
list.add(imageHolder0);
list.add(imageHolder1);
Container container = new Container(list);
Gson gson = new Gson();
String json = gson.toJson(container);
Intent intent = new Intent(MainActivity.this, TestActivity.class);
intent.putExtra("yourKey", json);
startActivity(intent);
}
}
Your second activity
public class TestActivity extends AppCompatActivity {
public static final String TAG = TestActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Gson gson = new Gson();
Container container = gson.fromJson(getIntent().getStringExtra("yourKey"), Container.class);
Log.d(TAG, "OK?" + container.getList().toString());
// Here convert String uri Fields to Uri Objects
// Example:
Uri uri = Uri.parse(container.getList().get(0).getUri());
}
}
Logcat result: 08-01 11:10:22.097 6671-6671/it.darksurfer.english.template D/TestActivity: OK?[ImageHolder{uri=http://www.stackoverflow.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test0 0,000000,0,000000 acc=??? t=?!? et=?!?]}, ImageHolder{uri=http://www.google.com, date=Wed Aug 01 11:10:21 GMT+02:00 2018, loc=Location[test1 0,000000,0,000000 acc=??? t=?!? et=?!?]}]
Obviously you can put an undefined number of others ArrayList in the list of Container class!
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.
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