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!!!