简体   繁体   中英

Trying to populate a ListView with results from a Parse query

I've got a simple app where users sign up and sign in, and I want them to be able to search for other users.

I'm using parse.com to store my users and their details, and have been looking into a way to search for users (using either names, phone numbers, emails, etc) and show them all in a list view, but i've come up empty. Most answers and tutorials involve using ParseQueryAdapter, but it looks like that class is no longer included in the SDK.

Does anyone have any experience with this?

Thanks for your time

EDIT: I've been messing around with ParseQuery, to try and get something. This is what I have so far:

 searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                ParseQuery<ParseUser> query = ParseUser.getQuery();
                //TODO search function

                query.whereContains("name", searchText.getText().toString().trim());
                query.findInBackground(new FindCallback<ParseUser>() {
                    @Override
                    public void done(List<ParseUser> objects, ParseException e) {
                        if (e == null) {
                            Log.d("Brand", "Retrieved " + objects.size() + " Brands");
                            for (ParseObject dealsObject : objects)
                            {
                                Log.i("USERNAME", dealsObject.getString("name"));
                                // use dealsObject.get('columnName') to access the properties of the Deals object.
                            }
                        } else {
                            Log.d("Brand", "Error: " + e.getMessage());
                        }
                    }
                });


            }
        });

This code outputs the number of results from the search as well as the "name" attribute for each test results to the log.

Additionally, I've already made a layout for each item in the list view, containing name, email, phone number and such details. Earlier, before I started using Parse, I was using a local MySQL database to store my users. I would then search for the user, which returned a JsonArray, and I would put each attribute into a custom "searchresult" object which had get() and set() methods for search attributes. After that I had an ArrayAdapter which I added the results to. This method was kind of inefficient, especially since I'm not an expert in PHP and Json.

you can try like this

query.whereContains("name", searchText.getText().toString().trim());
query.whereContains("phone", searchText.getText().toString().trim());
query.whereContains("emails", searchText.getText().toString().trim());

Found a way to do this. First, the search for any user matching any fields:

String searchParams = searchText.getText().toString().trim();

                if(!searchParams.isEmpty()) {
                    final ArrayList<SearchResults> searchResults = new ArrayList<SearchResults>();
                    listAdapter = new ListAdapter(activityContext, searchResults);
                    listView.setAdapter(listAdapter);

                    ParseQuery<ParseUser> queryName = ParseUser.getQuery();
                    queryName.whereContains("name", searchParams);
                    ParseQuery<ParseUser> queryPhone = ParseUser.getQuery();
                    queryPhone.whereContains("username", searchParams);
                    ParseQuery<ParseUser> queryEmail = ParseUser.getQuery();
                    queryEmail.whereContains("email", searchParams);
                    ParseQuery<ParseUser> querySurname1 = ParseUser.getQuery();
                    querySurname1.whereContains("surname1", searchParams);
                    ParseQuery<ParseUser> querySurname2 = ParseUser.getQuery();
                    querySurname2.whereContains("surname2", searchParams);

                    List<ParseQuery<ParseUser>> queries = new ArrayList<ParseQuery<ParseUser>>();
                    queries.add(queryName);
                    queries.add(queryPhone);
                    queries.add(queryEmail);
                    queries.add(querySurname1);
                    queries.add(querySurname2);

                    ParseQuery<ParseUser> mainQuery = ParseQuery.or(queries);

                    mainQuery.findInBackground(new FindCallback<ParseUser>() {
                        @Override
                        public void done(List<ParseUser> objects, ParseException e) {
                            if (e == null)
                            {
                                Log.d("Brand", "Retrieved " + objects.size() + " Brands");
                                for (ParseObject dealsObject : objects) {
                                    Log.i("USERNAME", dealsObject.getString("name"));
                                    Log.i("PRIVATE", Integer.toString(dealsObject.getInt("private")));
                                    SearchResults sr1 = new SearchResults();
                                    sr1.setName(dealsObject.getString("name"));
                                    sr1.setSurname1(dealsObject.getString("surname1"));
                                    sr1.setSurname2(dealsObject.getString("surname2"));
                                    sr1.setUid(dealsObject.getObjectId());
                                    Log.d("UID",dealsObject.getObjectId());

                                    if(dealsObject.getInt("private") == 0)
                                    {
                                        sr1.setEmail(dealsObject.getString("email"));
                                        sr1.setPhone(dealsObject.getString("username"));
                                    }
                                    else
                                    {
                                        sr1.setPhone("xxx-xxx-xxx");
                                        sr1.setEmail("xxxxxx@xxxxx.xxx");
                                    }

                                    searchResults.add(sr1);
                                }
                            } else {
                                Log.d("Brand", "Error: " + e.getMessage());
                            }

                            listAdapter.notifyDataSetChanged();

And my SearchResults class:

public class SearchResults
{
    private String name = "";
    private String surname1 = "";
    private String surname2 = "";
    private String phone = "";
    private String email = "";
    private Bitmap image;
    private String uid = "";

    public String getEmail() {
        return email;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getSurname1() {
        return surname1;
    }

    public String getSurname2() {
        return surname2;
    }

    public void setSurname1(String surname1) {
        this.surname1 = surname1;
    }

    public void setSurname2(String surname2) {
        this.surname2 = surname2;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM