我希望用户能够从列表中删除已添加的朋友。我已经设法修改了一些代码,但它删除了Parse“Friend”类中的第一个项目。如何修改它以仅删除所选用户?
@Override
public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) {
new MaterialDialog.Builder(MainActivity.this)
.title("Remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + "?")
.content("Do you really want to remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + " " + "from your friends list?")
.positiveText(R.string.dialog_yes)
.negativeText(R.string.dialog_no)
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onPositive(MaterialDialog dialog) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Friend");
query.whereEqualTo("user", mCurrentUser);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> user, ParseException e) {
user.get(position).deleteInBackground(new DeleteCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(getBaseContext(), "You removed" + " " + mUserFriends.get(position).getUsername().toUpperCase(), Toast.LENGTH_LONG).show();
mUserFriends.remove(position);
mFriendAdapter.notifyDataSetChanged();
} else {
Toast.makeText(getBaseContext(), "Couldn't remove" + " " + mUserFriends.get(position).getUsername().toUpperCase() + ", try again." + e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
});
}
@Override
public void onNegative(MaterialDialog dialog) {
}
})
.show();
return true;
}
答案 0 :(得分:0)
它可能是onItemLongClick
函数传递的位置参数的问题。大多数时候,函数传递的位置只是UI列表的位置而不是内容的位置。
mUserFriends.remove(position);
和
user.get(position).deleteInBackground
尝试获取元素的位置并尝试类似这样的
mUserFriends.remove(3); // for the remove line
和
user.get(position).deleteInBackground // for the get line in the function
然后您可以将这些来源用作参数,而不是onItemLongClick