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.
Related
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
I am trying to send an array of objects to a new intent, but the problem is that I don't know how to send the whole array at once. The method I managed to get it working is adding object by object, in a for loop, and in the second activity I have to get them one by one, something like this:
Main activity:
Intent newIntent = new Intent(mainA.this, secondA.class);
for(int i=0;i<numberOfObjects;i++)
newIntent.putExtra("object"+i,myObjects[i]);
newIntent.putExtra("length", x);
startActivity(newIntent);
Second activity:
Serializable n = getIntent().getSerializableExtra("length");
int x = Integer.parseInt(n.toString());
myObjects = new objClass[x];
for(int i=0;i<x;i++)
myObjects[i] = (objClass) getIntent().getSerializableExtra("object"+i);
Even if this method works, and gets me the right results, isn't there a better/faster/cleaner solution?(I searched a lot, but haven't found a better way, maybe I don't know what exactly to look for).
Make your object serializable and put all objects in a Arraylist and send it.ArrayList itself implements serializable.
Intent newIntent = new Intent(mainA.this, secondA.class);
newIntent.putExtra("list",listOfObjets);
startActivity(newIntent);
Second Activity:
ArrayList<YourObjects> list = (ArrayList<YourObjects>)getIntent().getSerializableExtra("list");
Hi you can try this as well.. Create setter getter class which will set and get your object. next create the array list of your setter getter class.next add all objects into arraylist using set method in for loop. next add that arraylist object into bundle using setArguments.
Same you can access that bundle using getArguments that's it.
I have this application with 2 classes, in the first class I define a list array wich I want to access in my second class, how do I do that? class one (with the array extends listActivity and the other class extends Activity). I don't think it's nessecary to post my code as I believe there is a quick solution to this I just don't know it. Allthough I can post it if you can't help me without seeing the actual code.
You can use a custom ApplicationContext to share global state between activities - see here for more details...
you can pass ArrayList<String> 's across activities using intentExtras.
ArrayList<String> myArrayList = new ArrayList<String>();
// populate your array -> if you want to send objects make a string form of them
Intent myIntent = new Intent(this, Activit2.class);
mtIntent.putStringArrayList("my_array_list", myArrayList);
In Activity2
Intent intent = getIntent();
List<String> stringMediaList = intent.getStringArrayListExtra("my_array_list");
for (int i = 0; i < stringMediaList.size(); i++) {
//reconstruct object if needed
}
this will work if you dont want to use parcelable
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.
I have a List in one of my activities and need to pass it to the next activity.
private List<Item> selectedData;
I tried putting this in intent by :
intent.putExtra("selectedData", selectedData);
But it is not working. What can be done?
Like howettl mentioned in a comment, if you make the object you are keeping in your list serializeable then it become very easy. Then you can put it in a Bundle which you can then put in the intent. Here is an example:
class ExampleClass implements Serializable {
public String toString() {
return "I am a class";
}
}
... */ Where you wanna create the activity /*
ExampleClass e = new ExampleClass();
ArrayList<ExampleClass> l = new ArrayList<>();
l.add(e);
Intent i = new Intent();
Bundle b = new Bundle();
b.putSerializeable(l);
i.putExtra("LIST", b);
startActivity(i);
You have to instantiate the List to a concrete type first. List itself is an interface.
If you implement the Parcelable interface in your object then you can use the putParcelableArrayListExtra() method to add it to the Intent.
i think ur item should be parcelable. and you should use arraylist instead of list.
then use intent.putParcelableArrayListExtra
This is what worked for me.
//first create the list to put objects
private ArrayList<ItemCreate> itemsList = new ArrayList<>();
//on the sender activity
//add items to list where necessary also make sure the Class model ItemCreate implements Serializable
itemsList.add(theInstanceOfItemCreates);
Intent goToActivity = new Intent(MainActivity.this, SecondActivity.class);
goToActivity.putExtra("ITEMS", itemsList);
startActivity(goToActivity);
//then on second activity
Intent i = getIntent();
receivedItemsList = (ArrayList<ItemCreate>) i.getSerializableExtra("ITEMS");
Log.d("Print Items Count", receivedItemsList.size()+"");
for (Received item:
receivedItemList) {
Log.d("Print Item name: ", item.getName() + "");
}
I hope it works for you too.
Everyone says that you can use Serializable, but none mentioned that you can just cast the value to Serializable instead of list.
intent.putExtra("selectedData", (Serializable) selectedData);
Core's lists implementations already implement Serializable, so you're not bound to a specific implementation of list, but remember that you still can catch ClassCastException.