I am facing a problem with sending data through the bundle.
Intent toAudio = new Intent(TourDescription.this, Audio.class);
toAudio.putParcelableArrayListExtra("poi", arraypoi);
startActivity(toAudio);
here am sending arraypoi which is an ArrayList. This ArrayList contains a set of values.
And in the receiving class, I have like this
listOfPOI = getIntent().getParcelableArrayListExtra("poi");
Collections.sort(listOfPOI);
where listOfPOI is also an array list.
The problem am facing is, I am not able to receive values for 3 particular variables in listOfPOI (coming as null), rest all values are coming proper.
While sending the bundle, I mean in arraypoi also I am able to send all the values correctly but the problem is while receiving it.
Note: My class is implemented as parcelable only.
Any answer for this?
There are two workarounds in my point of view.
First:
You need to make YourOwnArrayList as subclass of ArrayList<YourObject> and implements Parcelable.
Tutorial Here
Second:
pass your Object using for() loop. like this.
Intent toAudio = new Intent(TourDescription.this, Audio.class);
toAudio.putExtra("SIZE", arraypoi.size());
for(int i=0; i<arraypoi.size(); i++)
toAudio.putExtra("POI"+i, arraypoi.get(i));
startActivity(toAudio);
and while getting in other class
Bundle data = getIntent().getExtras();
int size=data.getInt("SIZE");
for(int i=0; i<size; i++)
listOfPOI.add((YOUR_OBJECT) data.getParcelable("POI"+i));
YOUR_OBJECT is the class name of your Object.
Related
My aim is to have a list of items retrieved via an api, have them listed on the page and then once clicked on, a new activity is opened where more information is shown.
I currently have the list printed out in a ListView where only the names of (in this case, films) are shown, but would like to tag each ListView item with the complete JSON including all other information such as Age etc so that this can be used as EXTRA information when summoning the new Intent.
My current code for looping through each film in the api is below:
for(int i=0; i<jsonArray.length(); i++){
JSONObject result = jsonArray.getJSONObject(i);
String filmTitle = result.getString("Title");
String filmYear = result.getString("Year");
String filmDesc = result.getString("Desc");
String filmPoster = result.getString("Poster");
Film data = new Film(filmTitle, filmYear, filmDesc, filmPoster);
ptResults.add(data);
}
Use Gson library to map JSON String to class u want... Film? Lets say UrClass
To list add ArrayAdapter with UrClass array and on item select (click) whatever get UrClass object from array and by Gson get again string and can send to other activity in extra field in Intent
Gson has method fromJson toJson
Make your Film class implement Parcelable. Then when the user selects an item from the ListView, the adapter already has the object to put into the Intent.
Parcelable classes can be easily written to and read from Intents for transferring entire objects between activities.
Parcelable is the Android interface for serialization. You could use Serializable, but the Parcelable implementation is written for each implementing class so no heavyweight reflection APIs are used.
Parecelable | Andoid Developers
I'm trying to transfer ArrayList between activities, but nothing I've tried works as well.
This was my best shot, but this didn't work either.
I'm calling the external action here:
getComics getComicInfo = new getComics(charName, pageNum);
getComicInfo.execute();
getIntentData()
and here i'm trying to put data, but the problem is due the fact that this is an external action, so I can't shift through activits.
if(counter == comicInfoObject.length()){
Log.v("check arr length =>" , Integer.toString(comicList.size()));
Intent i = new Intent();
i.putExtra("comicArrayList", (ArrayList<Comics>)comicList);
}
and here i'm tring to retrive the data, but it doesn't get inside the "if"
public void getIntentData(){
Intent i = getIntent();
if(i != null && i.hasExtra("comicArrayList")){
comicList2 = i.getParcelableExtra("comicArrayList");
int size = comicList2.size();
}
}
first code is where I call an external class that using api and in the bottom line creates the arrayList
second code is inside the external class, where I'm trying to pass the arrayList with putExtra
third code is where i'm tring to retrive the data after getIntentData().
Pack at the sender Activity
intent.putParcelableArrayListExtra(<KEY>, ArrayList<Comics extends Parcelable> list);
startActivity(intent);
Extract at the receiver Activity
getIntent().getParcelableArrayListExtra(<KEY>);
You need make a Comics class that implements Parcelable, then use put ParcelableArrayListExtrato pass arraylist.
Here is a sample link for Pass data between activities implements Parcelable
However be careful if your array list too big, you could get intent exception, for this case you could think about a static reference to store your array list.
I have an ArrayList<Long> object that i would like to retain on orientation change of my fragment. But the Bundle class does not have a putLongArrayList() method. I do see putParcelableArrayList(), putIntegerArrayList() and putStringArrayList().
Is there any reason why the Long version of the method is not defined? Is there a simple workaround for this other than creating wrapper parcelable class for the Long objects and then using putParcelableArrayList()?
I think this is the method you should use for that
You will have to convert the ArrayList to an Array before you're able to put it in the Bundle, though.
When you get the LongArray from the Bundle, you can convert it back to an ArrayList so you can perform list like operations on it again.
Putting in Bundle
long[] array = new long[yourArrayList.size()];
for(int i=0;i<yourArrayList.size();i++){
array[i] = yourArrayList.get(i);
}
Bundle b = new Bundle();
b.putLongArray("longs", array);
Getting from Bundle
long[] array = b.getLongArray("longs");
for(int i=0; i<array.length;i++) arrayList.add(array[i])
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