Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass ArrayList of Objects from one to another activity using Intent in android?

I have the following in code in my onClick() method as

 List<Question> mQuestionsList = QuestionBank.getQuestions(); 

Now I have the intent after this line, as follows :

  Intent resultIntent = new Intent(this, ResultActivity.class);   resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);   startActivity(resultIntent); 

I don't know how to pass this question lists in the intent from one activity to another activity My Question class

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();        }        } 
like image 878
RajeshVijayakumar Avatar asked Nov 28 '12 09:11

RajeshVijayakumar


People also ask

Can we pass object through intent in android?

One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent.


1 Answers

Between Activity: Worked for me

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

In the Transfer.class

Intent intent = getIntent(); Bundle args = intent.getBundleExtra("BUNDLE"); ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST"); 

Hope this help's someone.

Using Parcelable to pass data between Activity

This usually works when you have created DataModel

e.g. Suppose we have a json of type

{     "bird": [{         "id": 1,         "name": "Chicken"     }, {         "id": 2,         "name": "Eagle"     }] } 

Here bird is a List and it contains two elements so

we will create the models using jsonschema2pojo

Now we have the model class Name BirdModel and Bird BirdModel consist of List of Bird and Bird contains name and id

Go to the bird class and add interface "implements Parcelable"

add implemets method in android studio by Alt+Enter

Note: A dialog box will appear saying Add implements method press Enter

The add Parcelable implementation by pressing the Alt + Enter

Note: A dialog box will appear saying Add Parcelable implementation and Enter again

Now to pass it to the intent.

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

And on Transfer Activity onCreate

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

Thanks

If there is any problem please let me know.

like image 126
Somir Saikia Avatar answered Sep 22 '22 16:09

Somir Saikia