How to query from the Pointer in Parse - android

I have created a Pointer in ParseClass I want to query from the Pointer added I have created ParseObject class so how can set the ParseObject for querying the pointer.
Games Parse class has Pointer 'Places'
#ParseClassName("Games")
public class Offers extends ParseObject{
public String getPlaceName() {
return getString(getParseObject("Places").getString("PlaceName"));
}

If you want to get Games objects with related Places data, you should use 'include' in the query.
ParseQuery<Offers> query = ParseQuery.getQuery(Offers.class);
//query.equalTo("objectId", "gameId");
query.include("Places");
query.getFirstInBackground(new GetCallback<Offers>() {
#Override
public void done(Offers obj, ParseException e) {
if(e!=null) {
Log.e("err", e.getMessage());
}
else {
Log.d("PlaceName", obj.getParseObject("Places").getString("PlaceName"));
}
}
});
If you want to get related data by pointer(ParseObject which only contains id)
ParseObject place = ParseObject.createWithoutData("Places","id");
place.fetchInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
//do what you want
}
});

try it
final ArrayList<ParseObject> Places = new ArrayList<ParseObject>();
ParseQuery<ParseObject> offer = new ParseQuery<ParseObject>("className");
offer.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
// success
for (ParseObject parseObject : objects) {
Places.add(parseObject.getParseObject("Places"));
}
Log.e("TAG ", "get parse pointer object " + Places.size());
} else {
// fail
}
}
});

Related

Parse.com cannot get ParseRelation from Object

i try to retrieve relation from an object. but i get an error (This query has an outstanding network connection. You have to wait until it's done.).
I tried everything, but i get this error.
public void Member() {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"PrivateDialog");
try {
mFriendsRelation = query.get(relationid).getRelation("Member");
} catch (ParseException e1) {
e1.printStackTrace();
}
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
if (member.getAdapter() == null) {
member.setAdapter(new MemberAdapter(member.getContext(), mFriends, R.layout.member_group_item));
member.setHasFixedSize(true);
member.setLayoutManager(new LinearLayoutManager(GroupDialogActivity.this));
member.setItemAnimator(new DefaultItemAnimator());
} else {
// refill the adapter!
Log.i("Refill", "todo");
}
} else {
Log.e("TAG", e.getMessage());
}
}
});
}
});
}

Fetching relational data Android Parse.com

I am having a table structure like this trying to fetch multiple data all those related tables-
I have list of category (ProfileCateGory Object) and inside every category list (ProfileData Object) of profile Date with the there view count (ProfileViewCount Object)
I am new to this Parse.com I tried like this -
ParseQuery<ParseObject> query = ParseQuery.getQuery("ProfileData");
query.whereEqualTo("profileName", "Profile");
try {
for (int i = 0; i < query.count(); i++) {
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("ProfileData", "Error: " + e.getMessage());
} else {
Log.d("TAG", "Name: " + object.get("profileName").toString());
}
}
});
}
} catch (Throwable t) {
}
I am getting only one data from ProfileData.
You can use relational-queries
For that you have to make profileCode as Pointer type in ProfileViewCounts
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("ProfileData");
innerQuery.whereEqualTo("profileName", "Profile");
ParseQuery<ParseObject> query =ParseQuery.getQuery("ProfileViewCounts");
query.whereMatchesQuery("profileCode", innerQuery);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject objProfileViewCounts, ParseException e) {
// TODO Auto-generated method stub
}
});
getFirstInBackground() is to call and return just one item.
To get an array or more objets you need to do:
//profile or id of user , you have to pass as a parameter
ParseQuery<ParseObject> query = ParseQuery.getQuery("ProfileData");
query.whereEqualTo("profileName", Profile);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int inP = 0; inP < objects.size(); inP++) {
String profileName = objects.get(inP).getString("profileName");
}
} else {
}
}

parse.com update data android

I have an object at parse.com in the class Grillen with data.
Now I want to make it such that if I click a button it adds to the row with the same name as selected in a spinner, and it updates the columns Betrag and Rechnung. I tried the following code but it doesn't work.
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen")
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException e) {
if (e == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground();
}
}
});
}
Use this code to check what's going wrong
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
// Retrieve the object by id
query.getInBackground(String.valueOf(mNameInput), new GetCallback<ParseObject>() {
public void done(ParseObject Grillen, ParseException eg) {
if (eg == null) {
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
If you successfully save, then your exception is null, if it didn't you will know why via stacktrace. I suggest running this code and letting the execution flow of the app be based on whether or not the save was successful or not.
Read this: getInBackground(String, callback) You must pass the objectId of the object you are trying to get, rather than the name. Make sure you are getting by ObjectID and not name as it appears you are doing. ObjectId looks like a random string and should be found in your database by looking online on Parse
Do this instead:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", String.valueOf(mNameInput));
// Retrieve the object by id
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetragInput);
Grillen.put("Rechnung", false);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
Note: I have no idea what the column name is for the 'name' attribute of Grillen objects, so you might need to change this to whatever you call the name column for that object.
Okay i have get the Spinner to work for the name but not the EditText
here is some part of the codes
public class RechnungActivity extends AppCompatActivity {
private EditText mBetragInput;
private Spinner mNameInput;
private String mName;
private String mBetrag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechnung);
ParseObject.registerSubclass(Grillen.class);
mBetragInput = (EditText) findViewById(R.id.Sonstiges);
mNameInput = (Spinner) findViewById(R.id.Name);
mName = mNameInput.getSelectedItem().toString();
mBetrag = mBetragInput.getText().toString();
}
public void createRechnung(View v) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Grillen");
query.whereEqualTo("name", mName);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException eg) {
if (eg == null && objects.size() > 0) {
mBetrag = mBetragInput.getText().toString();
ParseObject Grillen = objects.get(0);
Grillen.put("Betrag", mBetrag);
Grillen.put("Rechnung", true);
Grillen.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
//success, saved!
Log.d("MyApp", "Successfully saved!");
} else {
//fail to save!
e.printStackTrace();
}
}
});
}else {
//fail to get!!
eg.printStackTrace();
}
}
});
}

Retrieve data from parse based on object ID?

I have a list of objectId where I am trying to retrieve another list in another class where the pointer value equals the objectId of the current parseObject.
I realised that I am comparing a string "objectId" to a pointer "threadPointer" so can someone help me with the query to retrieve a list of items from a class if the objectId equals the threadPointer _Pointer value
public void retrieveThreadListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.include("postedBy");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
public void retrievePostsListStoreInDB()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.include("postedBy");
query.include("threadPointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> list, ParseException e) {
if (e == null) {
// Query is successful now lets load data from parse
ParseObject.pinAllInBackground((List<ParseObject>) list, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
if (!isFinishing()) {
// TODO : Notify to refresh data
Log.d("DEBUG: ","Finished retrieval");
Log.d("DEBUG", list.toString());
ParseObject object = (ParseObject)list.get(1).get("threadPointer");
Log.d("DEBUG_THREAD_POINTER", object.getObjectId());
}
} else {
Log.d("DEBUG", "ERROR PINNING DATA WITH EXCEPTION : " + e);
}
}
});
}
}
});
}
This is where I am retrieving the ThreadList from local db
public void retrieveThreadList(){
List<ParseObject> parseObjectList = new ArrayList<ParseObject>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Threads");
query.orderByDescending("updatedAt");
query.fromLocalDatastore();
// Data retrieval was successfull
try {
parseObjectList= query.find();
} catch (ParseException e) {
e.printStackTrace();
}
for (ParseObject parseObject : parseObjectList) {
String title = (String) parseObject.get("threadTitle");
ParseUser parseUser = (ParseUser) parseObject.get("postedBy");
String profilePictureUrl="local";
try {
profilePictureUrl = ((ParseFile) parseUser.get("profilePicture")).getUrl();
}catch (Exception exception)
{
Log.d("DEBUG_FB","profilePicture not retrieved");
}
String threadObjectId = parseObject.getObjectId();
Log.d("DEBUG_FB_THREAD_ID",threadObjectId);
ParseQuery parseQueryToRetrievePosts = ParseQuery.getQuery("Posts");
parseQueryToRetrievePosts.fromLocalDatastore();
//----****** NEED HELP HERE ******----
parseQueryToRetrievePosts.whereEqualTo("threadPointer",parseObject.getObjectId());
try {
List<ParseObject> postsList = parseQueryToRetrievePosts.find();
Log.d("DEBUG_FB_POSTS_LIST",postsList.toString());
} catch (ParseException e) {
e.printStackTrace();
}
/* To retrieve data over the network --- THIS WORKS WELL BUT I DONT WANT A NETWORK CALL
ParseRelation <ParseObject> posts = parseObject.getRelation("postMessage");
ParseQuery postsQuery = posts.getQuery();
List<ParseObject> postsList = new ArrayList();
try {
postsList=postsQuery.find();
for (ParseObject parseObject1: postsList)
{
String postMessage = (String)parseObject1.get("postMessage");
Log.d("DEBUG_FB_MSG", postMessage);
}
Log.d("DEBUG_FB_POSTS", postsList.toString());
}catch (Exception e)
{
}
*/
threadModelStore threadObject = new threadModelStore(title,profilePictureUrl);
Log.d("DEBUG_FB", profilePictureUrl);
Log.d("DEBUG_FB", title);
threadItemList.add(threadObject);
}
Sir my scenario is I have these classes
THREADS(Class) which has POSTS(RELATION TO _POSTS) class I want to retrieve the related posts of a particular thread. I was unable to pin
relations to local data store please let me know if there is a way to
do so ? -- (UNSOLVED)
So what I want to do is to compare the thread pointer in the POSTS
class and retrieve the related posts. For this I needed to compare a
string object id to pointer field _ThreadPointer in POSTS Class
Suggest a possible solution -- SOLVED BY ( VED ).
ParseObject threadPointer=ParseObject.createWithoutData("Pointer_class_Name", "pointer_object_id");
ParseQuery<ParseObject> query=ParseQuery.getQuery("className");
query.whereEqualTo("_Pointer", threadPointer);
query.include("_Pointer");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> listCommunity, ParseException e) {
// TODO Auto-generated method stub
Log.d(TAG, "done");
if(e==null){
// access your data
}else{
// Error in retrieving data
}

Add two users to parse object and Query

I want to create a parse object with two users and query for the object using both users. My get other user method returns the other user i want to add to the group.
String id;
final ParseUser[] user = new ParseUser[1];
public void getOtherUser()
{
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "amanda");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
user[0] = objects.get(0);
addUserGroup();
} else {
// Something went wrong.
}
}
});
}
my add userGroup created a parse object with both users
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
After ive done this i want to be able to be able to update the parse object with new content and query for the object based on both users. The following mehtod is breaking on me though and im not sure why.
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
Its returning the following error
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Lcom.parse.ParseUser;
anybody know why this might be happening?
When you call ParseUser.getCurrentUser() parse return a user object. Now assuming you want to query usernames you need to change the ParseUser.getCurrentUser() to ParseUser.getCurrentUser().getUsername()
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser().getUsername());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser().getUsername() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
}

Categories

Resources