For example I have to pass name and roll number of 2 students in the following manner, abc 12 xyz 13
can anyone suggest how should I implement this?
For example I have to pass name and roll number of 2 students in the following manner, abc 12 xyz 13
can anyone suggest how should I implement this?
Create an Array of Strings like this:
Strings sa[] = new String[n];
for(int i = 0; i < n i++)
{
//name and rollNo are the Arrays of student names and roll nos you have
sa[i] = add(name[i] + "," + rollNo[i]);
}
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("StudentDetails", sa);
startActivity(intent);
In NextActivity:
String[] studentDetails = this.getIntent().getStringArrayExtra("StudentDetails");
and use Name and roll number using split on the item you wanted like:
String student1 = studentDetails[0].split(",")
String student1Name = student1[0];
int student1RollNo = Integer.parseInt(student1[0]);
And the simplest way it to give 2 Arrays, 1 for Names and 1 for Roll Numbers and use them with the same indexes
Create a class and make it implement Serializable import java.io.Serializable;
public class Record implements Serializable{
String name,rollNum;
public Record(String name, String rollNum) {
super();
this.name = name;
this.rollNum= rollNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRollNum() {
return rollNum;
}
public void setRollNum(String rollNum) {
this.rollNum = rollNum;
}
}
Now, when u need to pass the data to other activity, simply create an array or arrayList(coll_of_records) and add new records to them. After adding records,use
Intent intent = new Intent(Current_Activity.this, New_Activity.class);
intent.putExtra("xyz", coll_of_records);
startActivity(intent);
You do it that way. When you create the intent to start your new activity you can send "extra" to it before starting it
Intent myIntent;
int aInt = 10;
String aString = "a text";
myIntent = new Intent(view.getContext(),NewActivity.class);
myIntent.putExtra("My Integer Value", aInt);
myIntent.putExtra("My String Value", aString);
startActivity(myIntent);
Then in the new activity you fetch the values as follow
int aInt;
String aString;
aInt = getIntent().getExtras().getInt("My Integer Value");
aString = getIntent().getExtras().getString("My String Value");
Here I used long names "My Integer Value" to show you how to do it. This is the id of the passed value so keep it simple and logic as for example in your case passnum and rollnum.
Create Class Student and Set name,number to Object as per your fields...
Add all object in arraylist of (student)objects and send arraylist of objects via intent..
for this way you send multiple records to another activity...
Please refer below link same like as your question...