Bundle ArrayList <ArrayList<Integer>> [duplicate] - android

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.

Related

How to send user define object to other activity [duplicate]

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.

way to put arraylist in putExtras [duplicate]

This question already has an answer here:
Passing arraylist in intent from one activity to another
(1 answer)
Closed 6 years ago.
I want to send a ArrayList with intent for starting new activity.
I have something like this-
ArrayList<LatLng> positions = new ArrayList<LatLng>() ;
positions.add(new LatLng(12,13));
positions.add(new LatLng(13,14));
positions.add(new LatLng(14,15));
Intent intent = new Intent(Current.this,Next.class);
Now I want to carry this ArrayList named positions to the Next activity and want to use there. How can I achieve it.
LatLng class already implements Parcelable So you can directly pass this arraylist as
intent.putParcelableArrayListExtra("key", value)
you can use
intent.putExtra("array",positions.toArray());

how to pass the ArrayList<data class> from one activity to another activity android

In my application whenever we click the "SEARCH" button it makes Http call and get the json as response. I parsed the json and save the data in arraylist bdata.
ArrayList bdata = new ArrayList();
BusData contain getters and setters . I also used one adapter class for custom listview.
now i want to set the adapter to listview but the listview is in another class (not in the class where search button is locate) so how to send my arraylist from one activity to another activity with the help of intents for set the adapter to listview.
Thank you.
A way to do this is by serializing your arraylist to a JSON.
Use GSON library
compile 'com.google.code.gson:gson:2.3.1'
Add the above line in your build.gradle file.
Make a class to help you serializing your objects
public class SerializationHelper {
private static Gson gson = new Gson();
public static String serialize(Object object) {
return gson.toJson(object);
}
public static Object deserialize(String json, Class classType) {
return gson.fromJson(json, classType);
}
}
Now when you want to start the new activity add the serialized string.
String json=SerializationHelper.serialize(myArrayList);
intent.putExtra("data",json);
And in your new activity on create get it and create your object again.
String json=intent.getStringExtra("data");
Object deserializedObject=SerializationHelper.deserialize(json,ArrayList.class);
Now cast your object!
ArrayList<MyClass> myCoolArray=(ArrayList<MyClass>)deserializedObject.
Other simpler way is to make your arraylist static and public, and store it in an other class.
public class GlobalStuff{
public static ArrayList<MyClass> myAwesomeList;
}
Now acces your list by GlobalStuff.myAwesomeList.
You need to create custom class (ArrayList<data class>) to Parcelable and then you can pass it to other activity.
http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html
When passing data through activities you should use Parcelable, the are many examples in stackoverflow, this one is good:
https://stackoverflow.com/a/22446641/2091315
Next, you need to configure the intent, on the "sender" Activity:
Intent intent = new Intent(getApplicationContext(),
ReceiverActivity.class);
intent.putExtra("arrayListIdentifier",parcelableArrayClass);
startActivity(intent);
On the "receiver" Activity:
parcelableArrayClass myParcelableObject = (parcelableArrayClass) getIntent().getParcelableExtra("arrayListIdentifier");

How can I get access to a fragment method from another activity

I have a fragment which summons a custom CursorAdaper and display it on a ListView
. The thing is I want to change the cursor by changeCursor() from another activity when I add new data, How can I get access to the CursorAdapter displayed on the fragment?
Essentially you have to pass data from one Activity to another and let the fragment of you choice receive the data (each fragment of a given Activity may get the Intent that started/restarted/resumed the Activity).
Consider this code
-- pass data:
String[] myListEntries = getNewListContents();
Intent updateList = new Intent(this, ActivityThatListFragmentBelongsTo.class);
updateList.putExtra("updated_list", myListEntries);
startActivity(updateList);
-- receive data (in fragment):
#Override
public void onResume() {
Intent wasStartedWithData = getActivity().getIntent();
String[] updatedList = wasStartedWithData.getStringArrayExtra("updated_list");
// pass updatedList to adapter
}
Now you might actually have more complicated data than an Array of Strings. In this case
you could create a class that implements 'Parcelable' (http://developer.android.com/reference/android/os/Parcelable.html) and call
putExtra(Parcelable parcel) / getParcelableExtra(String name) on the Intent.

How to start an activity with custom values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hey guys I am reasonably new to Android programming but have some experience with .net anyway what I would like to do is create a class say RestartDialog then call this class from an activity. Normally in .net I would use:
RestartDialog rd = new RestartDialog();
rd.setType(EXTENDED_TYPE);
rd.show;
then it would start in extended mode however in Android you need Intents to start activitys and this is the only way am I right? I know I can use Intent.putExtra etc but I will need to set many values first.
What would be my best bet to achive this please? Thanks in advance for your help.
Intent is the way to send data. so in case you have to send many data, you can use Parcelable. It is way faster also..
If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster).
From the docs, a simple example for how to implement is:
// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
private int mData;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Observe that in the case you have more than one field to retrieve from a given Parcel, you must do this in the same order you put them in (that is, in a FIFO approach).
Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra():
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
Then you can pull them back out with getParcelableExtra():
Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");
You may also use GSON to send data..
First, you'll want to create an Intent:
Intent intent = new Intent();
Think of an intent as a way to store data values:
intent.putExtra("type", EXTENDED_TYPE);
When you're done putting information in your intent, you start the activity:
startActivity(intent);
Then, in your new activity, you extract the values you need in your onCreate method:
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_login_activity);
Intent intent = getIntent();
this.type = intent.getIntExtra("type", 0);
In this case, I've made getIntExtra return 0 if the extra "type" was not set.
Let me know if you have any other questions.
Though the easiest solution would be:
To create a class with static data members with getters setters.
set from one activity and get from another activity that object.
activity A
mytestclass.staticfunctionSet("","",""..etc.);
activity b
mytestclass obj= mytestclass.staticfunctionGet();

Categories

Resources