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

Jonathan Etienne
Jonathan Etienne
24,572 Points

Retrieving information from Parse

I am trying to retrieve information from parse. In particular, I have added a condition where it would only return the list of users that have selected the same activity.

I have tried the below code, but it does seem to work, as that condition returns an empty list.

ParseQuery<ParseUser> query = ParseUser.getQuery(); query.whereNotEqualTo("objectId", currentUserId); query.whereEqualTo("ActivityName",""); It would essentially see which users have selected that particular activity name and return

Below is the entire code

public class MatchingActivity extends Activity {

private String currentUserId; private ArrayAdapter<String> namesArrayAdapter; private ArrayList<String> names; private ListView usersListView; private Button logoutButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.matching);

    logoutButton = (Button) findViewById(R.id.logoutButton);
    logoutButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ParseUser.logOut();
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
        }
    });

    setConversationsList();
}

private void setConversationsList() {
    currentUserId = ParseUser.getCurrentUser().getObjectId();
    names = new ArrayList<String>();


    ParseQuery<ParseUser> query = ParseUser.getQuery();
   query.whereNotEqualTo("objectId", currentUserId);
    query.whereEqualTo("ActivityName",userActivitySelectionName);

    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                for (int i=0; i<userList.size(); i++) {
                    names.add(userList.get(i).getUsername().toString());
                }

                usersListView = (ListView)findViewById(R.id.usersListView);
                namesArrayAdapter =
                    new ArrayAdapter<String>(getApplicationContext(),
                        R.layout.user_list_item, names);
                usersListView.setAdapter(namesArrayAdapter);

                usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                        openConversation(names, i);
                    }
                });

            } else {
                Toast.makeText(getApplicationContext(),
                    "Error loading user list",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void openConversation(ArrayList<String> names, int pos) {
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("username", names.get(pos));
    query.findInBackground(new FindCallback<ParseUser>() {
       public void done(List<ParseUser> user, ParseException e) {
           if (e == null) {
               Intent intent = new Intent(getApplicationContext(), MessagingActivity.class);
               intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
               startActivity(intent);
           } else {
               Toast.makeText(getApplicationContext(),
                   "Error finding that user",
                       Toast.LENGTH_SHORT).show();
           }
       }
    });
}

} Thanks in advance.