-1

I have an if statement written below:

//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)));
    }

The problem is that every single time I run that statement it always returns FALSE for both if statements even though I know for a fact that 'friendsArray' & 'pendingFriendsArray' return TRUE in many circumstances.

Both arrays contain pointers to the _User table.

searchResultsList is declared as follows:

private List<ParseUser> searchResultsList;

I've logged all three items (friendsArray, pendingFriendsArray & searchResultsList.get(position)) to the console and they show the following:

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

The entire class is below:

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
    }
}
Brandon Stillitano
  • 1,304
  • 8
  • 27
  • I suggest that you debug it, as I don't think you provide clear enough information for anyone to help – Scary Wombat May 30 '18 at 05:17
  • What further information would be required? I have tried for hours to debug this but am unable to come to any concrete conlusion on what the cause is. – Brandon Stillitano May 30 '18 at 05:24
  • [Please add a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Scary Wombat May 30 '18 at 05:24
  • I believe I have done this. If you have any constructive feedback around how I could improve my response, as opposed to linking to a page, then I'd love to hear it so that I can improve my question. – Brandon Stillitano May 30 '18 at 05:35
  • OK lets break this down a bit a) what is the value of `position` b) what value does `searchResultsList.get(position)` return ? c) what contents does `ParseUser.getCurrentUser().getList("friendsArray")` return ? – Scary Wombat May 30 '18 at 05:38
  • I've included this inside the question. I did this as an edit just before you posted this response. Please see above. – Brandon Stillitano May 30 '18 at 05:39
  • Futhermore `com.parse.ParseUser@4d12c33` if you override `toString` then you can print the correct information – Scary Wombat May 30 '18 at 05:40
  • a) `ParseUser@4d12c33` does not seem to exist in your lists b) do you have a `ParseUser` havving a List of `ParseUser`s ? – Scary Wombat May 30 '18 at 05:45
  • a) The user doesn't exist in that case, it is probably a poor example on my part to have used in hindsight. b) Yes, in my _User class I have a column which is an array filled with pointers to the _User class. – Brandon Stillitano May 30 '18 at 05:47
  • Have you overriden `equals` in your `ParseUser` class? – Scary Wombat May 30 '18 at 05:51
  • To the best of my knowledge, no I have not. Is there a simple way I can check this? Also, I have updated my example of what is logged to show the issue happens with objects not inside my arrays. – Brandon Stillitano May 30 '18 at 05:53
  • OK your example is now better and we can see that the Object **is** there. Now *every single time I run that statement it always returns FALSE* - what does this mean? There is **no** boolean value being returned from your code. On StackOverflow, all of these issues should be sorted before posting your question, hence my requested that you [Please add a minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) – Scary Wombat May 30 '18 at 06:02
  • I use the word FALSE to represent that it does not run the if statement that you'd expect it to if the object was in the array. It always runs the final 'else' statement. – Brandon Stillitano May 30 '18 at 06:03
  • Please confirm that the logging you provided above is generated from debug statements immediately before //Set Friend Action OnClickListener & Image **and** is the output of `ParseUser.getCurrentUser().getList("friendsArray")` , `ParseUser.getCurrentUser().getList("pendingFriendsArray")` and `searchResultsList.get(position)` – Scary Wombat May 30 '18 at 06:12
  • That is correct. – Brandon Stillitano May 30 '18 at 06:13
  • In that case, IMO either you have overridden `equals` or you are not running this version of the code – Scary Wombat May 30 '18 at 06:15
  • Thank you a tonne for your help @ScaryWombat. It was truly appreciated. I have figured this issue out. It seems to be an issue within the parse-android SDK exclusively that doesn't appear in the iOS SDK. I'll post the explanation and my answer shortly. – Brandon Stillitano May 30 '18 at 06:22
  • What is your deal man...how did I waste your time? Whether I mentioned the iOS SDK or not, the issue is still Android related.....I was simply making an observation that it doesn't happen on the iOS SDK. You clearly weren't able to help and there is nothing wrong with that. Grow up. – Brandon Stillitano May 30 '18 at 08:06
  • Sorry I misread your comment. – Scary Wombat May 30 '18 at 08:14

1 Answers1

-1

Upon further investigation I found that the parse-android SDK does not fetch pointers the same every single time. For example when I fetch 'friendsArray', let's say right now, it will 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]

However if I then fetch it, let's say in 5 minutes, it will 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]

Additionally, I noted that even the pointer to searchResultsList.get(position) changes it's reference every time I loaded it.

The way I got around this was to create a function (seen below) that returns an array of the actual objectId's of the pointers inside the 'friendsArray'. This way I can guarantee that it will always be returning the same items and can therefore create an accurate 'contains' comparison.

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

I then run the following comparison to get the result I am looking for

if (FLKUserUtils.friendsArrayObjectIdsArray().contains(searchResultsList.get(position).getObjectId())) {
//Do something
}
Brandon Stillitano
  • 1,304
  • 8
  • 27