简体   繁体   中英

How to make ArrayList with model class data serializable in android between Activities?

I have a collection of data in ArrayList holding serializable model with following data types :-

  1. boolean isChecked
  2. String name
  3. String detail

I want to pass these datas from First Activity to Second Activity and edit the boolean state of isChecked using listView adapter with adapter callback .when I come back to First Activity I want to update the data from Second Activity.

I am able to pass data and Update it in second Activity but not able to get updated data in First Activity. How is it possible to achieve my requirement,Any kind of help is much appreciated Thank you

You can pass like this below

First Method

1) Implements your object class to serializable

public class Question implements Serializable

2) Put this in your 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);

3) Put this in your Target Activity

ArrayList<Question> questions = new ArrayList<Question>();
questions = getIntent().getSerializableExtra("QuestionListExtra");

Second Method

Notice You model class must implements Parcelable

List<Bird> birds = new ArrayList<Bird>();
//birds.add();
Intent intent = new Intent(Current.this, Transfer.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Birds", birds);
intent.putExtras(bundle);
startActivity(intent);

Recive like this

List<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");

You have to use startActivityForResult here. Need to pass your model arraylist again back to the activity one.

Refer the below code to achieve what you want.

How to pass data from 2nd activity to 1st activity when pressed back? - android

考虑将其序列化为字符串,将其作为额外的意图并在第2个活动中反序列化。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM