-1

我有一个 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)));
    }

问题是,每次我运行该语句时,它总是为两个 if 语句返回 FALSE,即使我知道 'friendsArray' 和 'pendingFriendsArray' 在许多情况下返回 TRUE 的事实。

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

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

整个班级如下:

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
    }
}
4

1 回答 1

-1

经过进一步调查,我发现 parse-android SDK 每次都不会获取相同的指针。例如,当我获取“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]

但是,如果我再取它,比如说在 5 分钟内,它会返回

[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) 的指针也会在我每次加载它时更改它的引用。

我解决这个问题的方法是创建一个函数(见下文),该函数返回“friendsArray”内指针的实际 objectId 数组。这样我可以保证它总是返回相同的项目,因此可以创建一个准确的“包含”比较。

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
}
于 2018-05-30T06:32:36.887 回答