List<ParseUser>.contains ParseUser 始终为 FALSE

List<ParseUser> .contains ParseUser always FALSE

我有一个 if 语句写在下面:

//Set Friend Action OnClickListener & Image
    if (ParseUser.getCurrentUser().getList("friendsArray").contains(searchResultsList.get(position))) {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_phone_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.green)));
    }
    else if (ParseUser.getCurrentUser().getList("pendingFriendsArray").contains(searchResultsList.get(position))) {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_check_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.gray_dark)));
    }
    else {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_person_add_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.colorPrimary)));
    }

问题是每次我 运行 那个语句它总是 return 两个 if 语句都是假的,即使我知道一个事实 'friendsArray' & 'pendingFriendsArray' return 在许多情况下为真。

两个数组都包含指向 _User 的指针 table。

searchResultsList 声明如下:

private List<ParseUser> searchResultsList;

我已将所有三个项目(friendsArray、pendingFriendsArray 和 searchResultsList.get(position))记录到控制台,它们显示以下内容:

D/friendsArray: [com.parse.ParseUser@ae66779, com.parse.ParseUser@8371cbe, com.parse.ParseUser@32d511f, com.parse.ParseUser@5fd2c6c, com.parse.ParseUser@7dd0235, com.parse.ParseUser@9c446ca, com.parse.ParseUser@5fe03b] 

D/pendingFriendsArray: [com.parse.ParseUser@7c6a358, com.parse.ParseUser@3688cb1, com.parse.ParseUser@480596]

D/searchResultsList.get(position) =: com.parse.ParseUser@5fe03b

整个class如下:

public class SearchUserAdapter extends RecyclerView.Adapter<SearchUserAdapter.ViewHolder> {
private Context searchContext;
private List<ParseUser> searchResultsList;
OnItemClickListener onItemClickListener;

public SearchUserAdapter(Context context, List<ParseUser> dataSet) {
    searchContext = context;
    searchResultsList = dataSet;
}

public interface OnItemClickListener {
    public void onItemClick(View view, ParseUser searchUserObject, int position);
}

public void setOnItemClickListener(final OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(searchContext).inflate(R.layout.ly_search_user, parent,false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    //Set User Name
    holder.txtUserName.setText(searchResultsList.get(position).getString("fullName"));

    //Set User Location
    holder.txtUserLocation.setText(GlobalFunctions.getParseUserLocationAsString(holder.txtUserName.getContext(), searchResultsList.get(position)));

    //Set User Profile Image
    if (searchResultsList.get(position).getParseFile("profilePicture") != null) {
        Glide.with(holder.imgUserProfilePicture.getContext()).applyDefaultRequestOptions(RequestOptions.circleCropTransform()).load(searchResultsList.get(position).getParseFile("profilePicture").getUrl()).into(holder.imgUserProfilePicture);
    }
    else {
        Glide.with(holder.imgUserProfilePicture.getContext()).applyDefaultRequestOptions(RequestOptions.circleCropTransform()).load(R.drawable.ic_profile_place_holder).into(holder.imgUserProfilePicture);
    }

    //Set Row OnClickListener
    holder.rlUserItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (searchResultsList.get(position).getObjectId().equalsIgnoreCase(ParseUser.getCurrentUser().getObjectId())) {
                Intent openProfile;
                openProfile = new Intent(holder.rlUserItem.getContext(), TimelineActivity.class);
                holder.rlUserItem.getContext().startActivity(openProfile);
            }
            else {
                Intent openOtherProfile = new Intent(holder.rlUserItem.getContext(), OtherUserTimelineActivity.class);
                openOtherProfile.putExtra("otherUserProfileId", searchResultsList.get(position).getObjectId());
                holder.rlUserItem.getContext().startActivity(openOtherProfile);
            }
        }
    });

    //Set Friend Action OnClickListener & Image
    if (ParseUser.getCurrentUser().getList("friendsArray").contains(searchResultsList.get(position))) {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_phone_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.green)));
    }
    else if (ParseUser.getCurrentUser().getList("pendingFriendsArray").contains(searchResultsList.get(position))) {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_check_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.gray_dark)));
    }
    else {
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_person_add_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.colorPrimary)));
    }
    holder.imgFriendAction.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            friendActionListenerAction(holder, searchResultsList.get(position));
        }
    });
}

private void friendActionListenerAction(ViewHolder holder, ParseUser parseUser) {
    if (ParseUser.getCurrentUser().getList("friendsArray").contains(parseUser)) {
        FLKCallUtils.showCallDialog(holder.imgFriendAction.getContext());
    }
    else if (ParseUser.getCurrentUser().getList("pendingFriendsArray").contains(parseUser)) {
        //Do nothing
    }
    else {
        //Add Friend
        FLKFriendUtils.sendFriendRequestFromUserToUser(ParseUser.getCurrentUser(), parseUser);

        //Update Image
        Glide.with(holder.imgFriendAction.getContext()).load(R.drawable.ic_check_black_24dp).into(holder.imgFriendAction);
        ImageViewCompat.setImageTintList(holder.imgFriendAction, ColorStateList.valueOf(searchContext.getColor(R.color.gray_dark)));
    }
}

@Override
public int getItemCount() {
    return searchResultsList.size();
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public MediumRobotoTextView txtUserName;
    public RegularRobotoTextView txtUserLocation;
    public RelativeLayout rlUserItem;
    ImageView imgUserProfilePicture;
    ImageView imgFriendAction;

    public ViewHolder(View itemView) {
        super(itemView);

        rlUserItem = (RelativeLayout) itemView.findViewById(R.id.rl_user_container);
        rlUserItem.setOnClickListener(this);

        txtUserName = (MediumRobotoTextView) itemView.findViewById(R.id.txt_user_name);
        txtUserLocation = (RegularRobotoTextView) itemView.findViewById(R.id.txt_user_location);

        imgUserProfilePicture = (ImageView) itemView.findViewById(R.id.img_profile_picture);
        imgUserProfilePicture.setOnClickListener(this);

        imgFriendAction = (ImageView) itemView.findViewById(R.id.img_friend_action);
        imgFriendAction.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //TODO - do something here if you wish
    }
}

经过进一步调查,我发现 parse-android SDK 并不是每次都获取相同的指针。例如,当我获取 'friendsArray' 时,比方说现在,它将 return

[com.parse.ParseUser@ae66779, com.parse.ParseUser@8371cbe, com.parse.ParseUser@32d511f, com.parse.ParseUser@5fd2c6c, com.parse.ParseUser@7dd0235, com.parse.ParseUser@9c446ca, com.parse.ParseUser@5fe03b]

但是,如果我随后获取它,假设 5 分钟后,它将 return

[com.parse.ParseUser@ec99877, com.parse.ParseUser@674bcg, com.parse.ParseUser@749hhc, com.parse.ParseUser@6fh3d6dg, com.parse.ParseUser@jdj8dk, com.parse.ParseUser@4c966ca, com.parse.ParseUser@3f0eeb]

此外,我注意到即使是指向 searchResultsList.get(position) 的指针也会在我每次加载它时改变它的引用。

我解决这个问题的方法是创建一个函数(如下所示),return 是 'friendsArray' 内指针的实际 objectId 数组。这样我可以保证它总是 return 相同的项目,因此可以创建准确的 'contains' 比较。

public static List<String> friendsArrayObjectIdsArray() {
    //Create Array of Friends
    List<ParseUser> friendsArray = ParseUser.getCurrentUser().getList("friendsArray");

    //Create Temp Array of Object Id's
    List<String> tempObjectIdsArray = new ArrayList<>();

    //Iterate List
    for (ParseUser friendUser : friendsArray) {
        tempObjectIdsArray.add(friendUser.getObjectId());
    }

    return tempObjectIdsArray;
}

我再运行下面的比较得到我要的结果

if (FLKUserUtils.friendsArrayObjectIdsArray().contains(searchResultsList.get(position).getObjectId())) {
//Do something
}