我有一个ListView,我将一个空的适配器传递给我,然后我从服务器拉出数据后填充:
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();
}
在我的getView方法中,我填充视图,我在其中一个视图上也有一个点击监听器,它从一个单独的列表中添加/删除项目,而不是填充适配器的那个:
@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;
}
出于某种原因,当我一遍又一遍地点击mBtnFavourite时,我得到一个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)
下面是我的AsyncTask代码:
private class SavePartyTask extends AsyncTask<Party, Void, Void> {
@Override
protected Void doInBackground(Party... params) {
new PartyController(mContext).saveParty(params[0]);
return null;
}
}
这是PartyController的代码:
公共类PartyController扩展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));
}
} 我此时感到困惑,并且一直试图弄清楚这一整天。 Plz帮助!!!
答案 0 :(得分:1)
我认为你有一个并发问题:
<li class="active">
在这里:
party.setMenuItems(modifiedRecipes);
new SavePartyTask().execute(party);
我不确定这两个函数是做什么的,但如果他们保留对列表的引用而不是复制,那么当UI线程修改按钮上的列表时,你可以访问后台线程中的列表点击。
这里一个简单的解决方案是在你的派对对象中复制你的列表,而不是保留对原始列表的引用:
PartyPlannerConfig.setCurrentParty(party);
mJobManager.addJobInBackground(new SavePartyJob(party));
确保此setMenuItems制作modifiedRecipes的副本,而不是保留对原始文件的引用。
答案 1 :(得分:1)
是的,它看起来像是一个并发问题。仔细调试代码,根据标志添加和删除列表中的项目。我想,在某个时刻你的代码试图删除一个元素,其中不存在。使用记录器跟踪列表中的项目,并检查是否要调用空列表。