Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Relating Users in Parse.com Adding Checkmarks When the List is Loaded

Friends arent checked?

So friends in the friends list arent showing up as checked. The relations are there in Parse.

Heres my addFriendCheckmarks():

private void addFriendCheckmarks() {
        mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                if (e == null) {
                    for (int i = 0; i > mUsers.size(); i++) {
                        ParseUser user = mUsers.get(i);

                        for (ParseUser friend : friends) {
                            if (friend.getObjectId().equals(user.getObjectId())) {
                                getListView().setItemChecked(i, true);
                            }
                        }
                    }

                } else {
                    Log.e(TAG, e.getMessage());
                }
            }
        });
    }

Any ideas?

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Orbyt,

As written, it doesn't look like your for loop will run:

 for (int i = 0; i > mUsers.size(); i++)

If you start with i = 0 and only run the for loop if i is greater than mUser.size() (with mUser size being greater than 0), then the for loop will not run.

I think you meant less than:

 for (int i = 0; i < mUsers.size(); i++)

I hope this helps.

Ah, somehow missed that. Thanks!

Joshua Silva
Joshua Silva
6,525 Points

Hi, after completing the project I realized I stumbled upon the same error myself. My code looks very much similar to video's but it still isn't working for me, also I realized something about this video and the last video. In the last video, "Storing Friend Relationships" the onListItemClick method has code for adding and removing a friend, i know this because a comment is marked in the EditFriendsActivity.java class with //remove friend and //add friend, however in this video the code looks to be placed in separate positions without any instruction on how to move the code from //add friend to //remove friend. It through me off so I went ahead and copied it as it isin this video, perhaps that may be the reason why it's not working for me. Anyhow, if you can please look at these lines of code for me that'd be great. Thanks!

@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id);

    if (getListView().isItemChecked(position)){
        //remove the friend
    }
    else {
        //add the friend
        mFriendRelation.add(mUsers.get(position));
        mCurrentUser.saveInBackground(new SaveCallback() {
            @Override
            public void done(ParseException e) {
                if (e!= null){
                    Log.e(TAG,e.getMessage());
                }
            }
        });
    }
}

private void addFriendCheckmarks(){
    mFriendRelation.getQuery().findInBackground(new FindCallback<ParseUser>(){
        @Override
        public void done(List<ParseUser> friends, ParseException e) {
            if (e == null){
                //list returned - look for a match
                for (int i = 0; i < mUsers.size(); i++) {
                    ParseUser user = mUsers.get(i);

                    for (ParseUser friend : friends) {
                        if (friend.getObjectId().equals(user.getObjectId())){
                            getListView().setItemChecked(i,true);
                        }
                    }
                }
            }
            else{
                Log.e(TAG,e.getMessage());
            }
        }
    });
}

}

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Joshua,

Can you make your own forum post. Also check the "Markdown Cheatsheet" right below the box where you type in your post to learn how to post code so that it is easier to read.

From what I can tell, the onListItemClick method is only halfway implemented. Perhaps you will finish the method in the next video?

If that doesn't help, feel free to make a new post and I or someone else will try to help.