Carlos Romero
Carlos Romero

Reputation: 51

Retrieving pointer data from Parse.com in Android

I'm trying to retrieve the data from a pointer in my parse.com database from the class User which field is "companyId" Pointer Company (It gets the company objectId from the class Company) to add that data in another table called Note into a field called companyId when creating an object.

This is my parse.com database:

Class Name: User
objectId String|companyId Pointer Company|username String |password String

Class Name: Note objectId String| companyId String |text String

I've look for solutions and I don't find anyone as I don't know how to retrieve the companyId value as it is a Pointer.

Thanks in advance for any repply.

EDIT: Problem solved, below there is how it worked for me and other way of solving it by "bigdee94".

Thanks!

Upvotes: 3

Views: 2211

Answers (3)

Venkatesh
Venkatesh

Reputation: 1024

I have created pointer object and i retrieved from that easily ..Below code

 final ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("Loading ...");
    progress.setCancelable(false);
    progress.show();
    ParseGeoPoint point = new ParseGeoPoint(l1, l2);
    ParseUser user = ParseUser.getCurrentUser();
    ParseObject shop = new ParseObject("ShopLocations");
    shop.put("locationName", locname.getText().toString());
    shop.put("pin", Integer.parseInt(setpin.getText().toString()) );
    shop.put("location", point);
    shop.put("menu", ParseUser.getCurrentUser()); // this is poniter object creation in User table
    shop.put("business", ParseUser.getCurrentUser()); // this is another pointer 
    shop.put("userobjectId", user.getObjectId());
    shop.saveInBackground();
    shop.saveInBackground(new SaveCallback() {

        @Override
        public void done(ParseException e) {
            // TODO Auto-generated method stub
            progress.dismiss();
            if (e == null) {
                Toast.makeText(Createloctaion.this, "success", Toast.LENGTH_SHORT).show();
                // success
            } else {
                customToast(e.getMessage());
            }
        }
    });

Below code for retrieving pointer object :

 if(ParseUser.getCurrentUser()!=null) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("ShopLocations");
        query.whereEqualTo("menu", ParseUser.getCurrentUser());
        query.whereEqualTo("business",ParseUser.getCurrentUser());
        query.whereEqualTo("userobjectId",ParseUser.getCurrentUser().getObjectId());
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> object, ParseException e) {
                if (e == null) {

                    if (object.size() > 0) {
                    for (ParseObject user : object) {

                        String chkbank = user.getString("locationName");
                        Toast.makeText(Dashboard.this, ""+chkbank, Toast.LENGTH_SHORT).show();
                    }
                    } else {
                    // Handle the exception
                    }
                } else {
                    Toast.makeText(Dashboard.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();

                }
            }
        });

    }

Upvotes: 0

Carlos Romero
Carlos Romero

Reputation: 51

One mate helped me and here is how we solved it:

Note n = new Note();
ParseUser user = ParseUser.getCurrentUser();
ParseObject userObj= user.getParseObject("companyId");
note.put("companyId", userObj.getObjectId().toString());

I founded kinda tricky as if you do try as:

Note n = new Note();
ParseUser user = ParseUser.getCurrentUser();
user.getParseUser("companyId");

It won't give the data in the field, I believe you get the memory access where the data is.

Thanks for your time, hope this helps more people!

Upvotes: 2

lawrenceagbani
lawrenceagbani

Reputation: 147

Carlos, you can try this : -
According to the docs, in order to call relational data from pointer columns you can use the include() method.

For example:-

ParseQuery<ParseObject> userQuery = ParseUser.getQuery();
userQuery.include("companyId");
userQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
   if (e == null) {
      // userObjects retrieved successfully
      ParseObject companyIdObject = (ParseObject) objects.get("companyObjectIdColumn");
   } else {
      Log.d("objects", "Error: " + e.getMessage());
   }
});



Where companyObjectIdColumn is the objectId column in you Company table(class) and
objects is the queried list of objects
P.S: you can make ParseObject companyIdObject a global variable so that you can access it from any where.

Upvotes: 4

Related Questions