简体   繁体   English

如何将多个记录从一个活动传递到android中的另一个活动?

[英]How to pass multiple records from one activity to another activity in android?

For example I have to pass name and roll number of 2 students in the following manner, abc 12 xyz 13 例如,我必须以以下方式传递2名学生的姓名和名册编号,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: 在NextActivity中:

String[] studentDetails = this.getIntent().getStringArrayExtra("StudentDetails");

and use Name and roll number using split on the item you wanted like: 并在想要的项目上使用split使用名称和卷号:

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 给出2个数组的最简单方法,其中1个用于名称,1个用于卷编号,并将它们与相同的索引一起使用

Create a class and make it implement Serializable import java.io.Serializable; 创建一个类并使其实现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. 现在,当您需要将数据传递给其他活动时,只需创建一个array或arrayList(coll_of_records)并向其中添加新记录。 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. 这是传递值的ID,因此请保持简单和逻辑,例如在您的情况下,passnum和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.. 将所有对象添加到(学生)对象的arraylist中,并通过intent发送对象的arraylist。

for this way you send multiple records to another activity... 通过这种方式,您可以将多个记录发送到另一个活动...

Please refer below link same like as your question... 请参考以下链接,就像您的问题一样...

Help with passing ArrayList and parcelable Activity 帮助传递ArrayList和parcelable Activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将两个字符串从一个活动传递到另一个活动 - How to pass two Strings from one Activity to another Activity in Android 将字符串从一个活动传递到另一个活动,Android - Pass String from one Activity to another activity, android 如何在Android中将用户输入从一项活动传递到另一项活动 - How to pass user input from one activity to another in Android MVP Android:如何将数据从一个活动传递到另一个活动? - MVP Android: how to pass data from one activity to another? 如何在Android Studio中将堆栈从一个活动传递到另一个活动 - How to pass a stack from one activity to another in android studio 如何在Android的同一活动中将日期从一个片段传递到另一个片段? - How to pass date from one fragment to another in the same activity in Android? 如何将BluetoothAdapter从一个活动传递到Android中的另一个活动? - How to pass a BluetoothAdapter from one activity to another in Android? 如何在Android上将对象从一个活动传递到另一个活动 - How to pass an object from one activity to another on Android 如何将文本从一个活动传递到另一个活动(android studio) - How to pass text from one activity to another activty (android studio) 如何将变量从一个活动传递到另一个活动? - How to pass variable from one activity to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM