Help with Passing Arraylist and Parcelable Activity

Help with passing ArrayList and parcelable Activity

I can see a number of problems here:

  1. Why use addressParcelable? Why not make address implement Parcelable, and then use:

    intent.putParcelableArrayListExtra( "addresses", addyExtras );
  2. Your parcelable object must include a static CREATOR. See the documentation for details.

  3. You are not actually adding any extras to the intent before you call startActivity(). See point 1 for a suggestion here.

I think that you will need to address all of these issues in order to get it working.

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

It works well,

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

In your Source Activity, use this :

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

In your Target Activity, use this :

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

Pass ArrayList ? implements Parcelable to Activity


  • The problem is in writing out to the parcel and reading in from the parcel ...

    @Override
    public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeInt(numOfSeason);
    dest.writeInt(numOfEpisode);
    }

    private void readFromParcel(Parcel in) {
    name = in.readString();
    numOfSeason = in.readInt();
    numOfEpisode = in.readInt();
    }
  • What you write out has to match what you read in...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this,SecondActivity.class);

    ArrayList<testparcel> testing = new ArrayList<testparcel>();

    i.putParcelableArrayListExtra("extraextra", testing);
    startActivity(i);
    }

    /**********************************************/


    public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
    }
    }
  • The above code is having onCreate() from two different activities. The first one launches the second one; and it works fine I was able to pull the parcelable without issue.

Passing ArrayList CustomObject Between Activities

This code

Bundle informacion= new Bundle();
informacion.putParcelableArrayList("eventos", ArrayList<Eventos> eventos);
intent.putExtras(informacion);

Should be

Bundle informacion = new Bundle();
ArrayList<Eventos> mas = new ArrayList<Eventos>();
informacion.putSerializable("eventos", mas);
intent.putExtras(informacion);

and Make sure your Eventos structure like a serializable object

private class Eventos implements Serializable {

}

Reading Values

ArrayList<Eventos> madd=getIntent().getSerializableExtra(key);

Passing arraylist of objects between activities

You should make your Song class implement Parcelable.

Parcelable with arraylist string passing value from activity to fragment android


    public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 999) {
@SuppressWarnings("unchecked")
ArrayList<String> ar1 = data.getStringArrayListExtra("key");
if(ar1.size()!= 0){
for (int i = 0; i < ar1.size(); i++) {
String value = ar1.get(i);
Toast.makeText(getActivity(),
"Path of array in home Fragment: "+ar1,
Toast.LENGTH_LONG).show();
} } } }

Missed this Line: ArrayList<String> ar1 = data.getStringArrayListExtra("key");
thanks for the help

How to pass ArrayList CustomeObject from one activity to another?

You can pass an ArrayList<E> the same way, if the E type is Serializable.

You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

Example:

ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);

In the other Activity:

ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");


Related Topics



Leave a reply



Submit