Robert
Robert

Reputation: 1192

How to do a query in Android using Parse?

My Class in parse is called "MyClass" and this one has several objects like

| ObjectId | Names | owners | users | (owners is a pointer of another class)

I want to do a query that gives me all of the names in my object "Names" that belong to the owner but, when I do this query i get this:

com.parse.ParseObject@41828fe0
com.parse.ParseObject@41829fdd
com.parse.ParseObject@4182aa28

my code is this

final ParseQuery query = new ParseQuery("MyClass");
query.whereEqualTo("owners", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback() {
    public void done( List<ParseObject> MyList, ParseException e) {

        if (e == null) {

            adapter = new ArrayAdapter<ParseObject>(MyActivity.this, android.R.layout.simple_list_item_1, MyList);
            listDev.setAdapter(adapter);

        }else{
            //error
        }

please help me how to do that query that gives me all of name that belong to the owner.

EDIT I found a solution and is something like this.

ParseQuery<ParseObject> query = ParseQuery.getQuery("MyClass");
        ParseUser user = ParseUser.getCurrentUser();
        query.whereEqualTo("owners", user);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, com.parse.ParseException e) {
                if (e == null) {
                    for (ParseObject parseObject : objects){
                        String task;
                        task= parseObject.get("owners").toString();
                        adapter.add(task);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    // Something went wrong.
                    Toast.makeText(getActivity(),"Error: " + e.getMessage().toString(),Toast.LENGTH_SHORT).show();
                }
            }
        });

Upvotes: 1

Views: 11516

Answers (3)

Alphay
Alphay

Reputation: 1

The solution is pretty easy, since you are getting com.parse.whatever, all u need to do is ParseUser user = ParseUser.getCurrentUser(); Then query.whereequalto("owner", user.getUsername());

The problem was, u where querying for currentUser instead of usernames.

Upvotes: 0

Joe
Joe

Reputation: 176

Your real problem here is your ArrayAdapter. You would need a custom adapter if you want to use Parse objects as your data type. The built in adapter doesn't know how to use Parse objects and is outputting the object as a string for you. Instead you should do something like

arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, MyNamesList);

Where MyNamesList is of type String.

It's hard to help on your query without more information but you are getting Parse objects back, you just need to get the name out of them with something like

MyList.get(i).getString("name");

Upvotes: 1

Lena Bru
Lena Bru

Reputation: 13947

somewhere outside the query method

    private ArrayList<YourObjectType> list;

public void done(List<ParseObject> objects, ParseException e) {
            if (objects == null || objects.size() == 0) {
                return; //no objects
            }
                list = new ArrayList<YourObjectType>();
            for (int i = 0; i < objects.size(); i++) {


                    YourObjectType myObject = new YourObjectType();
                    ParseObject object = objects.get(i);

                    myObject.objectId = object.getObjectId();
                    myObject.names = object.get("Names");
                    myObject.owners = object.get("owners");
                    myObject.users = object.get("users");
                    list.add(myObject);


            }



      adapter = new ArrayAdapter<YourObjectType>(MyActivity.this,android.R.layout.simple_list_item_1, MyList);
      listDev.setAdapter(adapter);
        }

now set the list to your adapter

your adapter should be able to handle objects of your type, so i think you need to create a custom adapter, the one provided by the android sdk, won't do here

Upvotes: 0

Related Questions