2

I have a ListView which I pass an empty adapter to, which I then populate once data is pull from the server:

Context mContext;
List<Recipe> menuList = new ArrayList<>();
private Party party;

public MenuAdapter(Context context) {
    mContext = context;
    party = PartyPlannerConfig.getCurrentParty();
}

public void replaceList(final @NonNull List<Recipe> menuList) {
    this.menuList = menuList;
    notifyDataSetChanged();
}

In my getView method, I populate views and I also have a click listener on one of the views which adds/removes items from a separate list from the one that's populating the adapter:

 @Override
public View getView(int position, View convertView, final ViewGroup parent) {
    final View view = LayoutInflater.from(mContext).inflate(R.layout.content_recipe_element, parent, false);

    final TextView mButton = (TextView) view.findViewById(R.id.name);
    final ImageView mImage = (ImageView) view.findViewById(R.id.image);
    final ImageView mBtnFavourite = (ImageView) view.findViewById(R.id.btn_favourite);

    final Recipe recipe = getItem(position);
    Picasso.with(mContext).load(recipe.getTableImageURL()).into(mImage);
    mButton.setText(recipe.getRecipeName());

    final List<Recipe> currentRecipes = new ArrayList<>(party.getMenuItems());
    final List<Recipe> modifiedRecipes = new ArrayList<>(party.getMenuItems());

    mBtnFavourite.setSelected(currentRecipes.contains(recipe));

    mBtnFavourite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                if (!mBtnFavourite.isSelected()) {
                    modifiedRecipes.add(recipe);
                } else {
                    if (modifiedRecipes.contains(recipe)) {
                        modifiedRecipes.remove(recipe);
                    }
                }
                party.setMenuItems(modifiedRecipes);
                notifyDataSetChanged();
                new SavePartyTask().execute(party);
            } catch (Exception ignore) { }
        }
    });

    mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mContext.startActivity(RecipeDetailActivity.makeIntent(mContext, recipe));
        }
    });

    return view;
}

For some reason, when I click mBtnFavourite over and over again, I get a NoSuchElementException:

AndroidRuntime:...
                  java.util.NoSuchElementException
                                                                               at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576)
                                                                               at com.backendless.FootprintsManager$Inner.updateFootprintForObject(FootprintsManager.java:257)
                                                                               at com.backendless.Persistence$4.handleResponse(Persistence.java:196)
                                                                               at com.backendless.async.message.AsyncMessage$ResponseHandler.handle(AsyncMessage.java:64)
                                                                               at com.backendless.async.message.AsyncMessage.handleCallback(AsyncMessage.java:41)
                                                                               at com.backendless.core.AndroidCarrier$1.handleMessage(AndroidCarrier.java:37)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Below is my AsyncTask code:

private class SavePartyTask extends AsyncTask<Party, Void, Void> {
    @Override
    protected Void doInBackground(Party... params) {
        new PartyController(mContext).saveParty(params[0]);
        return null;
    }
}

and this is the code for PartyController:

public class PartyController extends Controller {

private static PartyController instance;

public PartyController(Context context) {
    super(context);
}

public synchronized static PartyController getInstance(Context context) {
    if(instance == null) {
        instance = new PartyController(context);
    }
    return instance;
}

public void getPartyList() throws UserException {
    mJobManager.addJobInBackground(new GetPartyListJob());
}

public void addParty(String partyName, Occasion occasion, Moodboard inspiration) throws UserException {
    final Party party = new Party();
    final Long currentTime = (System.currentTimeMillis() / 1000L);

    party.setName(partyName);
    party.setCreated(currentTime);
    party.setOccasion(occasion);
    party.setMoodboard(inspiration);

    final Calendar c = Calendar.getInstance();
    party.setUpdated(c.getTimeInMillis());

    PartyPlannerLog.v(WHOAMI, "Logged in user: " + Backendless.UserService.loggedInUser());
    mJobManager.addJobInBackground(new SavePartyJob(party));
}

public void deleteParty(Party party) throws UserException {
    mJobManager.addJobInBackground(new DeletePartyJob(party));
}

public void saveParty(Party party) {
    final Calendar c = Calendar.getInstance();
    party.setUpdated(c.getTimeInMillis());
    PartyPlannerConfig.setCurrentParty(party);
    mJobManager.addJobInBackground(new SavePartyJob(party));
}

} I am beyond confused at this time and have been trying to figure this out for an entire day. Plz help!!!

The Hungry Androider
  • 2,158
  • 5
  • 24
  • 45

2 Answers2

1

I think that you have a concurrency issue:

party.setMenuItems(modifiedRecipes);
new SavePartyTask().execute(party);

which goes here:

PartyPlannerConfig.setCurrentParty(party);
mJobManager.addJobInBackground(new SavePartyJob(party));

I'm not certain what these 2 functions do, but if they keep a reference to the list instead of making a copy, then you could be accessing a list in your background thread while the UI thread modifies the list on the button click.

A simple solution here would be to make a copy of your list in your party object, rather than keeping a reference to the original one:

party.setMenuItems(modifiedRecipes);

make sure this setMenuItems makes a copy of modifiedRecipes instead of keeping a reference to the original.

Francesc
  • 13,308
  • 4
  • 34
  • 50
1

Yes, it looks like a concurrency issue . Debug your code carefully , where you are adding and removing items to the list based on flags . I guess, at a moment your code tries to remove an element , where there exists none . Keep a track of items in your list using logger , and check if you're invoking an empty List .

Saurabh Chaturvedi
  • 1,371
  • 1
  • 10
  • 27