0

I have a list userSubs (which I access with dm.theUser.getAllSubmissions) of items called Submission and a draft submission I call tempSub that is not in that list. I have a list view where I want to display all the items in userSubs and the tempSub.

I create a temporary ArrayList called toDisplay and add all of the items in userSubs and then append tempSub. I then pass toDisplay into my arrayAdapter. That works fine. But when I press back and the page reloads, my tempSub has somehow been added to userSubs... a thorough search of my own and an "find in path" search reveals that I never call the userSubs.add() function.

ArrayList<Submission> toDisplay = new ArrayList<>();
toDisplay = dm.theUser.getAllSubmissions();
if(dm.tempSub.draftCreated) {
    toDisplay.add(dm.tempSub);
}
saa = new submissionArrayAdapter(userSubmissions.this, toDisplay.toArray(
            new Submission[toDisplay.size()]));

Here is some of the array adapter code.

private final Activity context;
private Submission[] submissions;
//constructor
public submissionArrayAdapter(Activity context, Submission[] submissions) {

    super(context, R.layout.user_submission_row, submissions);
    this.context = context;
    this.submissions = submissions;

}

Any ideas on how tempSub is being added to userSubs?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
chargerstriker
  • 486
  • 1
  • 5
  • 20
  • Try change `toDisplay = dm.theUser.getAllSubmissions();` to `toDisplay.addAll(dm.theUser.getAllSubmissions());` Hope that helps! – i_A_mok May 16 '18 at 02:13

1 Answers1

1

I have a list userSubs (which I access with dm.theUser.getAllSubmissions) of items called Submission and a draft submission I call tempSub that is not in that list.

Here I assume that what you meant is:

userSubs = dm.theUser.getAllSubmissions);

I create a temporary arraylist called toDisplay and add all of the items in userSubs and then append tempSub.

I assume you're pointing to this:

ArrayList<Submission> toDisplay = new ArrayList<>();
toDisplay = dm.theUser.getAllSubmissions();
if(dm.tempSub.draftCreated) {
    toDisplay.add(dm.tempSub);
}

The problem with your code is, you're creating new variable toDisplay with

ArrayList<Submission> toDisplay = new ArrayList<>();

but then you're replacing the the newly created instance with:

toDisplay = dm.theUser.getAllSubmissions();

So, your toDisplay is not a new instance of ArrayList anymore. It's because your toDisplay is now referring to the instance returning by dm.theUser.getAllSubmissions();

When you use the following code:

toDisplay = dm.theUser.getAllSubmissions();
if(dm.tempSub.draftCreated) {
    toDisplay.add(dm.tempSub);
}

you're adding the item to the instance referred by dm.theUser.getAllSubmissions().

Please be noted, In Java, object is passed by reference in a method and method is returning a reference of object.

What you really need is copying the list from dm.theUser.getAllSubmissions(); to the toDisplay. You can use a shallow copy with something like this:

List<Submission> oldSubmissions = dm.theUser.getAllSubmissions();
ArrayList<Submission> toDisplay = new ArrayList<>(oldSubmissions);

For deep copy, please read How do you make a deep copy of an object in Java?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96