Search code examples
javaandroidserializationarraylistevent-bus

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


Solution

  • 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");