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

Adding a search to Ribbit

I'm trying to have the search ability added to the Ribbit Application. I don't want it so they go to see who the person can add and see 5000 user names lol.

I tried to use SearchView and I couldn't figure it out.

This is my code right now and it's probably horrible.

[code] package com.dvdvdr.reac;

import java.util.List;

import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText;

import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseUser;

public class QueryFriendsActivity extends Activity {

//search user
protected EditText sUser;
//search button
protected Button sButton;

protected List<ParseObject> sSearchedUser;

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

    sUser = (EditText)findViewById(R.id.searchField);
    sButton = (Button)findViewById(R.id.searchButton);
    sButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String searchedUser = sUser.getText().toString();

            searchedUser = searchedUser.trim();

            if (searchedUser.isEmpty()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(QueryFriendsActivity.this);
                builder.setMessage(R.string.search_error_message)
                    .setTitle(R.string.search_error_title)
                    .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                //search for the user!
                setProgressBarIndeterminateVisibility(true);
                //searchedUser.search


                ParseQuery<ParseObject> query = ParseQuery.getQuery(searchedUser);
                query.whereEqualTo("username", searchedUser);
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> searchList, ParseException e) {
                        if (e == null) {
                            sSearchedUser = searchList;

                            String[] usernames = new String[sSearchedUser.size()];
                            int i = 0;
                            for(ParseObject user : sSearchedUser) {
                                usernames[i] = user.getClassName();
                                i++;
                            }
                            /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                    getListView().getContext(), 
                                    android.R.layout.simple_list_item_checked,
                                    usernames);
                            setListAdapter(adapter);*/
                        } else {
                            Log.d("username", "Error: " + e.getMessage());
                        }
                    }
                });

            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.query_friends, menu);
    return true;
}

}[/code]

NOTE: it doesn't work and I can't figure out how to make it work.

1 Answer

can anyone help?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

I missed this one - are you still stuck? If so, what kind of error message are you getting?

i want to add a searchview to the editfriends activity...but i dont know how to start...i want to know how to query parse for that.