I'm trying to make a parse query from my activity. It's working fine, i'm able to retrieve my objects but there are always empty (ie match.user1 always null)
ParseUser user = ParseUser.getCurrentUser();
ParseQuery<Match> query = ParseQuery.getQuery(Match.class);
query.whereEqualTo("user1", user);
query.include("user1");
query.include("user2");
query.findInBackground(new FindCallback<Match>() {
#Override
public void done(List<Match> list, ParseException e) {
if(e == null){
// list[0].user1 = null
}
}
});
In my ApplicationController
ParseObject.registerSubclass(Match.class);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XXX", "XXX");
What's wrong in my code ?
Thanks
Try get the references object (in your case columns 'user1' and 'user2') as following:
list.get(0).getParseObject('user1')
this give you the reference object then you can access it's values (columns) user the getters methods
Related
i do this in my onCreate method:
Parse.enableLocalDatastore(this);
Parse.initialize(this);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("Becky", "is dumb");
testObject.put("SammyHuang", "is smart");
testObject.saveInBackground();
then i have a button does this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SammyHuang");
query.getInBackground("2wTGHJ230q", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your game score
becky = object.getString("Becky");
sammy = object.getString("SammyHuang");
System.out.println(sammy);
}
else {
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
// something went wrong
}
}
});
i copy the object id "2wTGHJ230q" from the parse website and tried to get it display in terminal but overtime i run it it prints "bruh", i can't figure what went wrong. thx in advanced.
edit:
i fix the getQuery("SammyHuang") to getQuery("TestObject"), it still prints "bruh".
re edit:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class MainActivity extends AppCompatActivity {
private String sammy;
private String becky;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// [Optional] Power your app with Local Datastore. For more info, go to
// https://parse.com/docs/android/guide#local-datastore
Parse.enableLocalDatastore(this);
Parse.initialize(this);
setContentView(R.layout.activity_main);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("Becky", "is dumb");
testObject.put("SammyHuang", "is smart");
testObject.saveInBackground();
}
public void helloFunc(View view){
final TextView lol = (TextView)findViewById(R.id.moron);
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
query.getInBackground("u2VE9tIAwA", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
becky = new String(object.getString("Becky"));
sammy = new String(object.getString("SammyHuang"));
} else {
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
e.printStackTrace();
System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbruh");
// something went wrong
}
}
});
lol.setText("becky " + becky + "\n" + "sammy " + sammy);
}
}
i don't know why the lol.setText give me null. seems like assign the variable "becky" and "sammy" within the method getinbackground is not working. and also i am confused about how can this method has a parameter that is a object with another method define within it.
You save the object as a TestObject (when you do new ParseObject("TestObject")) so you also need to retrieve it as one by doing ParseQuery.getQuery("TestObject").
I think it would be a good idea for you to check the Parse documentation on saving and retrieving objects.
And in case you run into an error again, make sure to check the message of the ParseError, as it usually contains a great deal of information that can help you find the issue.
I am creating an app in android that allows the user to search other users registered in the app.
Each user is asked to enter a set of details like "Date of Birth" before entering into the app.These data are directly stored in the user class.
My requirement is that if a user enters a username and searches he must be able to see all the details available on that particular user.
For eg.:Lets assume every user must have a email(String) and date of birth(String).An User searches for name "John". What should the query be so that I can retrieve both email and DOB of a user with username "John".
The data given in the Parse.com guide is
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", name);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(),"Query Success",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Query Not Successful",Toast.LENGTH_LONG).show();
}
}
});
What are the changes that I need to make so that I can achieve my goal?
How to access the string stored inside the object?
If I retrieve multiple records from my query how can I handle it?
I have edited my query like this
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereMatches("username", name);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
for(ParseUser singleobject:objects){
String mail=singleobject.get("email").toString();
Toast.makeText(getApplicationContext(),mail,Toast.LENGTH_LONG).show();}
} else {
Toast.makeText(getApplicationContext(),"Query Not Successful",Toast.LENGTH_LONG).show();
}
}
});
No toast is generated but the query is executed without any exception.
Is the looping statement correct or am i doing something wrong??
What are the changes that I need to make so that I can achieve my goal?
The code looks like it correctly queries ParseUser for an exact match of a username. To match more loosely, consider using whereMatches
How to access the string stored inside the object?
Parse.Object provides getString() as well as other "get" variations depending on the attribute type.
If I retrieve multiple records from my query how can I handle it?
The "objects" parameter of the findInBackground completion handler is an array that contains zero or more matches to the query. Handle this array the way you would any other.
I have a parse class named "classA". I want to build a query where I want to search for rows in "classA" having certain "objectId". I have written the following code. But it always returns an empty list of ParseObjects. I have tried with separate columns existing in "classA". All the time, empty list returned. Please help.
I have used this query before on another class "classB" inside the same application and that works perfectly. I have also tried, "try-catch" block implementation using "query.find". That also returns empty list. Thanks in advance. Please explain what's wrong.
Need more code snippets, please let me know.
Button btn = (Button) findViewById(R.id.query_btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText objIdET= (EditText) findViewById(R.id.obj_id);
String objId= objIdET.getText().toString();
Log.d("check", objId); // Prints correct value here..
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("classA");
fridgeID.whereEqualTo("objectId", objId);
List<ParseObject> lp = new ArrayList<ParseObject>();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
Log.d("#results", String.valueOf(objects.size()));
}
});
}
}
});
Replace FridgeId with query
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("classA");
query.whereEqualTo("objectId", objId);
List<ParseObject> lp = new ArrayList<ParseObject>();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
Log.d("#results", String.valueOf(objects.size()));
}
});
Use getInBackground() if you have objectId of that row.
ParseQuery<ParseObject> query = ParseQuery.getQuery("classA");
query.getInBackground(objId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
Log.d("#results", String.valueOf(objects.size()));
} else {
// Handle the error
}
}
}
Call findInBackground() if you don't have objectId and setting some condition. You can also set condition on objectId field like this.
ParseQuery<ParseObject> query = ParseQuery.getQuery("classA");
query.whereEqualTo("objectId", objId);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
Log.d("#results", String.valueOf(objects.size()));
} else {
// Handle the error
}
}
}
You are getting an empty list of Parse objects because there aren't any rows corresponding to the user (which you are logged in with).
Parse.com implements privacy of its data by using a field called ACL. So if user A adds an entry to your table, by default the ACL is set to Read+Write for User A and none for other users. So user B will not be able to find any objects of userA.
To read more : https://parse.com/docs/android/guide#security
If you want to create a mechanism by which only users belonging to a certain group can read+write each other's objects, you can Roles (Parse feature).
https://parse.com/docs/android/guide#roles
Check if you have set the correct keys when you initialize the parse in your application.
If you want to get list of users, and ParseQuery.getQuery doesn't work - try this:
val query: ParseQuery<ParseUser> = ParseUser.getQuery()
query.findInBackground() {...}
I want to retrieve data from parse.com and show it in listview. but when i retrieve it and it shows me in hashcode. I have pasted picture of output.Please look into my query code and correct me.
Here is code for pulling data from parse.com
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> list, ParseException e) {
if (e != null) {
Toast.makeText(getActivity(), "Error" + e, Toast.LENGTH_SHORT).show();
} else {
for (ParseUser user : list) {
ParseUser newUser = new ParseUser();
newUser.getString(user.getString("username"));
newUser.getString(user.getString("Latitude"));
newUser.getString(user.getString("Longitude"));
newUser.getString(user.getString("Bloodtype"));
newUser.getString(user.getString("Mobile"));
user1.add(newUser);
}
ArrayAdapter<ParseUser> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,user1);
setListAdapter(adapter);
}
}
});
ArrayAdapter just use the toString of each object in the array.
Every user that you add to list is displayed as a pointer, this is the to string method of the ParseUser object.
If you are trying to display all information about the user you should create a custom adapter and implement the getView method.
If you want to check for now if it works correctly you can pass a list of just username for example and check if it works, then you can move on and implement your own adapter.
Edit:
For creating custom adapter check this question
Using the Parse SDK for android, I'm trying to receive a ParseUser from a query, the following code is what I have tried:
ParseQuery<ParseUser> query = ParseUser.getQuery();
Follow.include("author");
query.whereEqualTo("User", "Alex");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// This is where I want to receive the ParseUser from the List<ParseUser>
ParseUser pu = objects.get("author");
} else {
}
} });
Object.get("author") creates errors.
Thankyou for reading :)
ParseUser pu = objects.get("author"); would be incorrect usage. Get method is generally used to get a object at a particular position in the list.
The query returns a list of ParseUsers which you have named as objects.
Now use an iterator to go through the list to access each ParseUser.
like this
for(ParseUser pu : objects){
//access the data associated with the ParseUser using the get method
//pu.getString("key") or pu.get("key")
}
Hope this helps!