I want a list object that not allow duplicate value, which Set<String> is what I need.
However it can't pass to other activity via bundle or extra because it's not serializable?
how can I achieve this ?
Set<String> foo = new HashSet<String>();
Intent i = new Intent();
i.putExtra("key", foo);
startActivity(..., i);
Edit
Instead of..
Set<String> foo = new HashSet<String>();
Change to this..
HashSet<String> foo = new HashSet<String>();
Problem Solved Thank to all .
What are you talking about...
public class
HashSet
extends AbstractSet<E>
implements Set<E> Cloneable Serializable
Hash set is serializable... You need not pass in a Set, just use a HashSet OR if you want to STILL use a Set, as the datatype, then cast it to HashSet when its passed in the intent.
Ref
http://developer.android.com/reference/java/util/HashSet.html
You can do something like this:-
In the activity from which you want to send data
ArrayList userlist= new ArrayList();
Intent intent = new Intent(context, Dashboard.class);
intent.putExtra("userlist", userlist);
The activity in which you want to get the data
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
userlist = (ArrayList<User>) getIntent().getSerializableExtra("userlist");
}
The User Bean
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -3200348961839049924L;
/**
*
*/
private String name;
private String phoneNo;
private String emailId;
}
Related
The activity that i want to pass values is main activity-menu of app. From this activity i do startActivity the all activity with buttons. I want to retrieve values from two different activities at the same time as I mentioned to the title and when I startActivity each actity i want to send values.Any help?
Use Static fields or methods
Have Static String fields (or methods to compute and then return strings) in all classes that you want to get values from, then have your activity-menu get them and pass them into the your startActivity intent.
Example:
class activity-menu extends Activity{
public void StartActivityC(){
// Get value from Activity_A
string value_A = Activity_A.myString;
// Get value from Activity_B
string value_B = Activity_B.myString;
// Store both values in an Intent:
Intent intent = new Intent(this, Activity_C.class);
// store the string from Activity A under "value_A"
intent.putExtra("value_A", value_A);
// store the string from Activity B under "value_B"
intent.putExtra("value_B", value_B);
startActivity(intent);
}
}
public class Activity_A extends Activity{
public static String myString;
public Activity_A(){
myString = "this is from activity A";
}
}
public class Activity_B extends Activity{
public static String myString;
public Activity_B(){
myString = "this is from activity B";
}
}
public class Activity_C extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the Intent that started this activity and extract the strings:
Intent intent = getIntent();
String value_A = intent.getStringExtra("value_A");
string value_B = intent.getStringExtra("value_B");
// DONE!
}
}
could someone please answer my question. in my activity class.java, I have an intent and i want to send an object using intent. when I searched for it, the results were I have to implement parcelable to the class "object" that I want to send it. I did that but the thing is I want to put two objects to be sent to main2activity.java, when I tried to do that, my app crashed, when I debugged it said that main2activity has much intent? so my question is how can I send two objects using put extra, and get them in the other java using getintent.getparcelableextra()?
mainactivity.java
clickedplace is an object of class called Place
Intent myintent = new Intent(getApplicationContext(), localpopup.class);
myintent.putExtra("localprice",clickedplace.getTicketId().getLocalPrice());
myintent.putExtra("placeobject", clickedplace.getId());
main2activity.java
localpriceplace of type double
getpressplace of type Place
localpriceplace= getIntent().getParcelableExtra("localprice");
getpressedplace= (Place) getIntent().getParcelableExtra("placeobject");
You should be able to do this by creating a Bundle and putting each Intent into it with a String key. In "main2activity" you should retrieve them by name.
But the bundle into a new Intent and send that to main2activity
Otherwise, if you are putting data into each Intent and then sending the Intent directly to main2activity, you will need to implement "onNewIntent" in main2activity to get the new data.
Hi Please follow below link which is useful to you and solved your problem.
http://www.vogella.com/tutorials/AndroidParcelable/article.html
Hope above link helps you.
If you want to auto generate those parcelable model class then follow below link which saves your time.
https://github.com/mcharmas/android-parcelable-intellij-plugin
Please check below code:
1) You need to create your model class as a parcelable follow by above link.
2) To pass data to other activity:
intent.putExtra("student", new Student("1","Mike","6") // which is your model class object passing);
3)GetData into Other Activity Using intent:
Bundle data = getIntent().getExtras();
Student student = (Student) data.getParcelable("student");
// after you get all data from student.
Hope you understand.
Try this implements Parcelable to your Place Model
Intent myintent = new Intent(getApplicationContext(), localpopup.class);
myintent.putExtra("placeModel",clickedplac);
Receive
placeModel= getIntent().getParcelableExtra("placeModel");
String price=placeModel.getTicketId().getLocalPrice();
String Id=placeModel.getId()
ActivityB.java
public class ActivityB {
private static final String ARG_DATA1 = "Data1";
private static final String ARG_DATA2 = "Data2";
public static startActivityB (Context context, Data1 data1, Data2 data2) {
Bundle bundle = new Bundle();
bundle.putParcelable(ARG_DATA1, data1);
bundle.putParcelable(ARG_DATA2, data2);
Intent intent = new Intent(context, ActivityB.class);
intent.putExtras(bundle);
context.startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
Data1 data1 = bundle.getParcelable(ARG_DATA1);
Data2 data2 = bundle.getParcelable(ARG_DATA2);
}
}
Data1.java
public static class Data1 implements Parcelable {
public static final Creator<Data1> CREATOR = new Creator<Data1>() {
#Override
public Data1 createFromParcel(Parcel in) {
return new Data1(in);
}
#Override
public Data1[] newArray(int size) {
return new Data1[size];
}
};
protected Data1(Parcel in) {
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
}
and Data2.java should also implement Parcelable.
And lastly, to start ActivityB from ActivityA,
Data1 data1;
Data2 data2;
ActivityB.startActivityB(ActivityA.this, data1, data2);
I'm begginer in Android and i try to pass object between activities with object contains other objects but errors appear.
A solution?
I try to pass object with serializable :
lALL.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v,
int position, long arg3) {
System.out.println(position);
Locataire l = new Locataire(getResources());
l.setNom("test");
l.setPrenom("test");
Intent intent = new Intent(MainActivity.this, Etat_lieux.class);
intent.putExtra("EDL", l);
startActivity(intent);
}
});
And
public class Locataire implements Serializable{
private String ref;
private String civilite;
private String nom;
private String prenom;
private Contact contact = new Contact();
private Adresse adresse = new Adresse();
private String DG;
private boolean IsGarantPresent;
private boolean IsColocation;
private Resources res;
public Locataire(Resources res)
{
this.res = res;
}
Contact,Adresse
public class Contact implements Serializable{
protected String tel;
protected String mobile;
protected String fax;
protected String email;
protected String www;
And ressource is Context getResources() of first activity
If you want to pass data between activities in your own app you should use startActivity, startActivityForResult and onActivityResult. You create a Bundle object and encapsulate your data in it with putExtra. Then you retrieve this object in your onCreate method.
http://developer.android.com/training/basics/intents/result.html
Otherwise, if you want to pass data between activities in your app and activities in another app you should use explicit or implicit intents.
http://developer.android.com/training/basics/intents/sending.html
If you want to pass object in extras... you have to make the object implements serializable..and make all it sub-objects implement serializalbe..
then use bundle.putSerializable(OBJECT)..
I have searched a few topics but not found a solution to my problem.
public class Series implements Parcelable {
private String name;
private int numOfSeason;
private int numOfEpisode;
/** Constructors and Getters/Setters have been removed to make reading easier **/
public Series(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.name = data[0];
this.numOfSeason = Integer.parseInt(data[1]);
this.numOfEpisode = Integer.parseInt(data[2]);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.name,
String.valueOf(this.numOfSeason),
String.valueOf(this.numOfEpisode) });
}
private void readFromParcel(Parcel in) {
name = in.readString();
numOfSeason = in.readInt();
numOfEpisode = in.readInt();
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
#Override
public Series createFromParcel(Parcel in) {
return new Series(in);
}
#Override
public Series[] newArray(int size) {
return new Series[size];
}
};
}
In my MainActivity I have an ArrayList. To make the list dynamically editeable I need to pass it to another activity where I can edit it.
ArrayList<Series> listOfSeries = new ArrayList<Series>();
public void openAddActivity() {
Intent intent = new Intent(this, AddActivity.class);
intent.putParcelableArrayListExtra(
"com.example.episodetracker.listofseries",
(ArrayList<? extends Parcelable>) listOfSeries);
startActivity(intent);
}
I need to cast the list, otherwise Eclipse gives me the following Error message.
The method putParcelableArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, List)
Is this the correct way to do it?
ArrayList<Series> list = savedInstanceState
.getParcelableArrayList("com.example.episodetracker.listofseries");
This is the way I try to read the data in another activity.
It's crashing on the line above. namely the getParcelableArrayList part.
The problem is in writing out to the parcel and reading in from the parcel ...
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(numOfSeason);
dest.writeInt(numOfEpisode);
}
private void readFromParcel(Parcel in) {
name = in.readString();
numOfSeason = in.readInt();
numOfEpisode = in.readInt();
}
What you write out has to match what you read in...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this,SecondActivity.class);
ArrayList<testparcel> testing = new ArrayList<testparcel>();
i.putParcelableArrayListExtra("extraextra", testing);
startActivity(i);
}
/**********************************************/
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
}
}
The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.
You should use the putParcelableArrayListExtra() method on the Intent class.
I've used putParcelableArrayList(<? extends Parcelable>) from a Bundle Object. Not directly from an Intent Object.(I don't really know what's the difference). but i use to use in this way:
ArrayList<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
resultSet = loadData();
Bundle data = new Bundle();
data.putParcelableArrayList("search.resultSet", resultSet);
yourIntent.putExtra("result.content", data);
startActivity(yourIntent);
Later on your new activity you can populate the data recently inserted on the Bundle object like this:
Bundle data = this.getIntent().getBundleExtra("result.content");
ArrayList<ParcelableRow> result = data.getParcelableArrayList("search.resultset");
Just remember that your ArrayList<> must contain only parcelable objects. and just to make sure that your have passed the data you may check if the data received is null or not, just to avoid issues.
Maybe this helps someone.. else my problem was that I used write and readValue but it should match type like writeInt, readInt writeString, readString and so forth
I am doing it in this way:
var intent = Intent(this#McqActivity,ResultActivity::class.java)
intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
// resultList is of type mutableListOf<ResultBO>()
startActivity(intent)
And for making the class Parcelable . I simply do two operation first I used #Parcelize Annotation above my data class and secondly I inherit it with Parcelable like below..
import kotlinx.android.parcel.Parcelize
import android.os.Parcelable
#Parcelize // Include Annotion
data class ResultBO(val questionBO: QuestionBO) : Parcelable {
constructor() : this(QuestionBO())
}
And at receiving end
if (intent != null) {
var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
}
You can pass Parcelable ArrayList
Sender Activity ->
startActivity(Intent(this, SenderActivity::class.java).apply { putExtra("getList",list)})
Receiver Activity ->
private lateinit var list: ArrayList<List>
list = this.intent.extras?.getParcelableArrayList("getList")!!
And you will get all Arraylist in list.
How to pass Collections like ArrayList, etc from one Activity to another as we used to pass Strings, int with help of putExtra method of Intent?
Can anyone please help me as i want to pass a List<String> from one Activity to another?
You can pass an ArrayList<E> the same way, if the E type is Serializable.
You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).
First you need to create a Parcelable object class, see the example
public class Student implements Parcelable {
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
}
public Student(Parcel in) {
id = in.readInt();
name = in.readString();
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
public Student createFromParcel(Parcel in) {
return new Student(in);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
}
And the list
ArrayList<Student> arraylist = new ArrayList<Student>();
Code from Calling activity
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);
this.startActivity(intent);
Code on called activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle bundle = getIntent().getExtras();
ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
use putExtra to pass value to an intent. use getSerializableExtra method to retrieve the data
like this
Activity A :
ArrayList<String> list = new ArrayList<String>();
intent.putExtra("arraylist", list);
startActivity(intent);
Activity B:
ArrayList<String> list = getIntent().getSerializableExtra("arraylist");
I tried all the proposed techniques but none of them worked and stopped my app from working and then I finally succedded. Here is how I did it...
In main activity I did this:
List<String> myList...;
Intent intent = new Intent...;
Bundle b=new Bundle();
b.putStringArrayList("KEY",(ArrayList<String>)myList);
intent_deviceList.putExtras(b);
....startActivity(intent);
To get the data in the new Activity:
List<String> myList...
Bundle b = getIntent().getExtras();
if (b != null) {
myList = bundle.getStringArrayList("KEY");
}
I hope this will help someone...
To pass ArrayList from one activity to another activity, one should include
intent.putStringArrayListExtra(KEY, list); //where list is ArrayList which you want to pass
before starting the activity.And to get the ArrayList in another activity include
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
temp = bundle.getStringArrayList(KEY); // declare temp as ArrayList
}
you will be able to pass ArrayList through this.Hope this will be helpful to you.
Use of ViewModel is better fix than Serializable or Parcelable