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

Android Relating users in parse.com. I can't select a friend from the list

i recursively check the tutorial and l am not getting where l am doing wrong. my EditFriendActivity.java is here

public class EditFriendsActivity extends ListActivity {
    public static final String TAG = EditFriendsActivity.class.getSimpleName();
    protected List<ParseUser> mUsers;
    protected ParseRelation<ParseUser> friendRelation;
    protected ParseUser currentUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_edit_friends);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        //getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        currentUser = ParseUser.getCurrentUser();
        friendRelation = currentUser.getRelation(ParseConstants.KEY_FRIEND_RELATION);

        setProgressBarIndeterminateVisibility(true);
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.orderByAscending(ParseConstants.KEY_USERNAME);
        query.setLimit(1000);
        query.findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> users, ParseException e) {
                setProgressBarIndeterminateVisibility(false);
                // TODO Auto-generated method stub
                if(e==null){
                    mUsers = users;
                    String[] usernames = new String[mUsers.size()];
                    int i = 0;
                    for(ParseUser us :mUsers){
                        usernames[i]=us.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            EditFriendsActivity.this,
                            android.R.layout.simple_list_item_checked,
                            usernames);
                    setListAdapter(adapter);
                    addFriendCheckmarks();

                } else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendsActivity.this);
                    builder.setMessage(e.getMessage())
                            .setTitle(R.string.error_title)
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_edit_friends,
                    container, false);
            return rootView;
        }
    }
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    if(getListView().isItemChecked(position)){
        //add friend
        friendRelation.add(mUsers.get(position));

    } else {
        //remove friend
        friendRelation.remove(mUsers.get(position));    
    }
    currentUser.saveInBackground(new SaveCallback() {

        @Override
        public void done(ParseException e) {
            // TODO Auto-generated method stub
            if(e!=null){
                Log.e(TAG, e.getMessage());
            }       
        }
    });
}
private void addFriendCheckmarks(){
    friendRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {

        @Override
        public void done(List<ParseUser> friends, ParseException e) {
            // TODO Auto-generated method stub
            if(e==null){
                for(int i =0;i <mUsers.size();i++){
                    ParseUser usr = mUsers.get(i);
                    for(ParseUser fr : friends){
                        if(fr.getObjectId().equals(usr.getObjectId())){
                            getListView().setItemChecked(i,true);
                        }
                    }
                }
            }else{
                Log.e(TAG, e.getMessage());
            }
        }
    }); 
}
}

8 Answers

The only difference I can see between your code and mine is the existence of the Fragment class in yours. I don't see why that should affect the checkmark at all, though.

You can see all my code here - this isn't my project, but it is in working order.

Steve

Hi there,

Can you specify what your question is?

  • What works, what doesn't work as expected?
  • Is the issue with communicating with Parse, or the local app not working?
  • Does your information reach Parse?
  • Does all/any of it get from Parse to the app?
  • Does the code build & compile without warnings?
  • Have you tried adding breakpoints to work out which line isn't working?
  • What did you change last, i.e. which code stopped the app from working?
  • You have some Log.e statements in there - do they write anything to the log?

Just a few points to help us understand where you're up to with this project.

Steve.

Code is compiling and running fine. the problem comes when selecting a friend, it is not highlighting or selecting a friend. i am able to communicate to parse.com but the relation cell doesn't appear after l try to select multiple friends.

Right, so the list of friends is in Parse and it is the addition of the checkmark that fails - does the relation exist in the Parse back-end, though? Is it just the checkmark, or is it the addition of the relation, programatically, that is failing? Or the save to back-end?

yes its about the checkmark. When l add this line

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

in the mainActivity l could not select any friends remains the same. The relation doesn't exist in the parse back-end too. So how can l solve this?

I'll have a look now we've understood what the problem is. I'm getting my version of Ribbit from Github to see if the code is different.

You have the CHOICE_MODE_MULTIPLE line in there twice; once commented out.

the other CHOICE_MODE_MULTIPLE is a comment

thanks , there was this nested class below l removed and worked fine.

public static class PlaceholderFragment extends Fragment {

Yes, that's the class I mentioned - how strange it caused that to malfunction!

I'm glad you got it sorted.

Steve.

Steve, why is it my code doesn't create a folder for external storage device

if(!mediaStorageDir.exists()){
                    if(!mediaStorageDir.mkdir()){
                        Log.e(TAG, "Failed to create a directory!");
                        return null;
                    }
                }

it returns Failed to create a directory.

Is the method mkdirs() with the 's' added?

Steve.

thanks. it worked

No problem! :-)