Raj*_*mar 27 android parcelable android-intent
我在我的onClick()方法中的代码中有以下内容
List<Question> mQuestionsList = QuestionBank.getQuestions();
Run Code Online (Sandbox Code Playgroud)
现在我有了这一行后的意图,如下:
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);
startActivity(resultIntent);
Run Code Online (Sandbox Code Playgroud)
我不知道如何将这个问题列表从一个活动传递到另一个活动My Question类
public class Question {
private int[] operands;
private int[] choices;
private int userAnswerIndex;
public Question(int[] operands, int[] choices) {
this.operands = operands;
this.choices = choices;
this.userAnswerIndex = -1;
}
public int[] getChoices() {
return choices;
}
public void setChoices(int[] choices) {
this.choices = choices;
}
public int[] getOperands() {
return operands;
}
public void setOperands(int[] operands) {
this.operands = operands;
}
public int getUserAnswerIndex() {
return userAnswerIndex;
}
public void setUserAnswerIndex(int userAnswerIndex) {
this.userAnswerIndex = userAnswerIndex;
}
public int getAnswer() {
int answer = 0;
for (int operand : operands) {
answer += operand;
}
return answer;
}
public boolean isCorrect() {
return getAnswer() == choices[this.userAnswerIndex];
}
public boolean hasAnswered() {
return userAnswerIndex != -1;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// Question
builder.append("Question: ");
for(int operand : operands) {
builder.append(String.format("%d ", operand));
}
builder.append(System.getProperty("line.separator"));
// Choices
int answer = getAnswer();
for (int choice : choices) {
if (choice == answer) {
builder.append(String.format("%d (A) ", choice));
} else {
builder.append(String.format("%d ", choice));
}
}
return builder.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
Som*_*kia 58
活动之间:为我工作
ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在Transfer.class中
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");
Run Code Online (Sandbox Code Playgroud)
希望这有帮助的人.
使用Parcelable在Activity之间传递数据
这通常在您创建DataModel时有效
例如假设我们有一个类型的json
{
"bird": [{
"id": 1,
"name": "Chicken"
}, {
"id": 2,
"name": "Eagle"
}]
}
Run Code Online (Sandbox Code Playgroud)
这里的鸟是一个List,它包含两个元素
我们将使用jsonschema2pojo创建模型
现在我们有模型类Name BirdModel和Bird BirdModel包含Bird和Bird的List包含name和id
转到bird类并添加接口" implements Parcelable "
通过Alt + Enter在android studio中添加implementsmets方法
注意:将出现一个对话框,指出Add implements方法按Enter键
按Alt + Enter添加Parcelable实现
注意:将出现一个对话框,说明添加Parcelable实施并再次输入
现在把它传递给意图.
List<Bird> birds = birdModel.getBird();
Intent intent = new Intent(Current.this, Transfer.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Birds", birds);
intent.putExtras(bundle);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
并且关于转移活动onCreate
List<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");
Run Code Online (Sandbox Code Playgroud)
谢谢
如果有任何问题,请告诉我.
小智 23
脚步:
将您的对象类实现为可序列化
public class Question implements Serializable`
Run Code Online (Sandbox Code Playgroud)把它放在你的Source Activity中
ArrayList<Question> mQuestionList = new ArrayList<Question>;
mQuestionsList = QuestionBank.getQuestions();
mQuestionList.add(new Question(ops1, choices1));
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", mQuestionList);
Run Code Online (Sandbox Code Playgroud)把它放在你的目标活动中
ArrayList<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Questions>) getIntent().getSerializableExtra("QuestionListExtra");
Run Code Online (Sandbox Code Playgroud)Raj*_*mar 18
效果很好,
public class Question implements Serializable {
private int[] operands;
private int[] choices;
private int userAnswerIndex;
public Question(int[] operands, int[] choices) {
this.operands = operands;
this.choices = choices;
this.userAnswerIndex = -1;
}
public int[] getChoices() {
return choices;
}
public void setChoices(int[] choices) {
this.choices = choices;
}
public int[] getOperands() {
return operands;
}
public void setOperands(int[] operands) {
this.operands = operands;
}
public int getUserAnswerIndex() {
return userAnswerIndex;
}
public void setUserAnswerIndex(int userAnswerIndex) {
this.userAnswerIndex = userAnswerIndex;
}
public int getAnswer() {
int answer = 0;
for (int operand : operands) {
answer += operand;
}
return answer;
}
public boolean isCorrect() {
return getAnswer() == choices[this.userAnswerIndex];
}
public boolean hasAnswered() {
return userAnswerIndex != -1;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// Question
builder.append("Question: ");
for(int operand : operands) {
builder.append(String.format("%d ", operand));
}
builder.append(System.getProperty("line.separator"));
// Choices
int answer = getAnswer();
for (int choice : choices) {
if (choice == answer) {
builder.append(String.format("%d (A) ", choice));
} else {
builder.append(String.format("%d ", choice));
}
}
return builder.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的源活动中,使用此:
List<Question> mQuestionList = new ArrayList<Question>;
mQuestionsList = QuestionBank.getQuestions();
mQuestionList.add(new Question(ops1, choices1));
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);
Run Code Online (Sandbox Code Playgroud)
在您的目标活动中,使用此:
List<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
Run Code Online (Sandbox Code Playgroud)
通过Parcelable传递对象。这是一个很好的入门指南。
第一个问题应该这样实现Parcelable并添加以下行:
public class Question implements Parcelable{
public Question(Parcel in) {
// put your data using = in.readString();
this.operands = in.readString();;
this.choices = in.readString();;
this.userAnswerIndex = in.readString();;
}
public Question() {
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(operands);
dest.writeString(choices);
dest.writeString(userAnswerIndex);
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
@Override
public Question[] newArray(int size) {
return new Question[size];
}
@Override
public Question createFromParcel(Parcel source) {
return new Question(source);
}
};
}
Run Code Online (Sandbox Code Playgroud)
然后像这样传递您的数据:
Question question = new Question();
// put your data
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra("QuestionsExtra", question);
startActivity(resultIntent);
Run Code Online (Sandbox Code Playgroud)
并像这样获取您的数据:
Question question = new Question();
Bundle extras = getIntent().getExtras();
if(extras != null){
question = extras.getParcelable("QuestionsExtra");
}
Run Code Online (Sandbox Code Playgroud)
这样就可以了!
小智 5
您的bean或pojo类应该implements parcelable interface。
例如:
public class BeanClass implements Parcelable{
String name;
int age;
String sex;
public BeanClass(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public static final Creator<BeanClass> CREATOR = new Creator<BeanClass>() {
@Override
public BeanClass createFromParcel(Parcel in) {
return new BeanClass(in);
}
@Override
public BeanClass[] newArray(int size) {
return new BeanClass[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeString(sex);
}
}
Run Code Online (Sandbox Code Playgroud)
考虑这样一个场景,你要发送arraylist的beanclass从类型Activity1到Activity2。
使用以下代码
活动1:
ArrayList<BeanClass> list=new ArrayList<BeanClass>();
private ArrayList<BeanClass> getList() {
for(int i=0;i<5;i++) {
list.add(new BeanClass("xyz", 25, "M"));
}
return list;
}
private void gotoNextActivity() {
Intent intent=new Intent(this,Activity2.class);
/* Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)list);
intent.putExtra("BUNDLE",args);*/
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("StudentDetails", list);
intent.putExtras(bundle);
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
活动2:
ArrayList<BeanClass> listFromActivity1=new ArrayList<>();
listFromActivity1=this.getIntent().getExtras().getParcelableArrayList("StudentDetails");
if (listFromActivity1 != null) {
Log.d("listis",""+listFromActivity1.toString());
}
Run Code Online (Sandbox Code Playgroud)
我认为这是基本了解的概念。
就那么简单 !!为我工作
从活动
Intent intent = new Intent(Viewhirings.this, Informaall.class);
intent.putStringArrayListExtra("list",nselectedfromadapter);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
TO 活动
Bundle bundle = getIntent().getExtras();
nselectedfromadapter= bundle.getStringArrayList("list");
Run Code Online (Sandbox Code Playgroud)
小智 5
使用 Intent 传递 ArrayList 的最简单方法
在依赖块 build.gradle 中添加这一行。
implementation 'com.google.code.gson:gson:2.2.4'
Run Code Online (Sandbox Code Playgroud)
通过数组列表
ArrayList<String> listPrivate = new ArrayList<>();
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putExtra("private_list", new Gson().toJson(listPrivate));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在另一个活动中检索列表
ArrayList<String> listPrivate = new ArrayList<>();
Type type = new TypeToken<List<String>>() {
}.getType();
listPrivate = new Gson().fromJson(getIntent().getStringExtra("private_list"), type);
Run Code Online (Sandbox Code Playgroud)
您也可以在类型中使用 object 而不是 String
为我工作..
| 归档时间: |
|
| 查看次数: |
94585 次 |
| 最近记录: |