get the ParseFile from the ParseQuery - android

I am writing an address book APP and using the Parse service.
I also use the listview and BaseAdapter to implement the APP
There are many people I saved as the ParseObject.
And each ParseObject includes a photo saved as file.
I get all ParseObjects by :
ArrayList<ParseObject> list = new ArrayList<ParseObject>();
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("member");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
list = (ArrayList<ParseObject>) objects;
mMyadapter = new MyAdapter(context, list);
}
else {
}
});
in the adapter, I want to show the image from the ParseFile
My question is :
How do I get the ParseFile from the list?
I tried below code but the APP will shut down suddenly.
I wrote the getView method in the MyAdapter like below:
ParseFile pf = (ParseFile) list.get(position).get("photo1");
pf.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] bytes, ParseException e) {
if (e == null) {
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
viewTag.imageView1.setImageBitmap(bm);
}
else {
Log.d("debug", "bad");
}
}
});
I can not use the getUrl() neither....
Can someone tell me how to modify it? Many thanks for your help!

You don't need to initialize list.
How are you setting the adapter of your ViewGroup? At the moment, you're creating a new adapter only when your ParseQuery is done. Your adapter may not be set properly.
Since list could point to new ArrayList<ParseObject>() or (ArrayList<ParseObject>) objects depending on whether ParseQuery is done, it's possible that you're calling list.get() on an invalid position.
In short, make your member variables local. Also, if MyAdapter is an anonymous class, pull that code out into a static inner class so it doesn't refer to its containing class' member variables. Use getItem() to access your ParseObjects, not list.get().

Related

Android Parse query returning Empty List

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() {...}

Retrieved data from parse.com but it is in HashCode

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

Relational Query Parse.com android

So I'm Implementing an app using Parse.com as back end, There is basically 3 class in Parse. The first one User where I have the User's Information, and Gallery where I have images "like Instagram, and Finally Follow Class where I have the relation following and followers.
Now I have an activity where I have to display all the images for the people I'm following only. I couldn't write a correct relational queries to get the correct result.
so this is the code that display all the images in the database not only for the people I'm following.
public class FollowingImages extends ParseQueryAdapter<ParseObject> {
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.include("createdBy");
return query;
}
});
}
// Customize the layout by overriding getItemView
#Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item, null);
}
super.getItemView(object, v, parent);
// retrieving the Images from Parse.com
return v;
}
I don't know where should I apply the relational query in the constructor or inside getItemView.
please any help would be highly appreciated.
You will want to apply constraints to your query.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Gallery");
query.whereEqualTo("createdBy", "theUserId");
query.findInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("score", "The find request failed.");
} else {
Log.d("score", "Retrieved the object.");
}
}
});
See the official guide for more details.
You could use the matchesKeyInQuery constraint method.
You can use the matchesKeyInQuery method to get objects where a key matches the value of a key in a set of objects resulting from another query.
(after parse docs)
So in your case (I am writing it by hand) it may be something like:
ParseQuery<Follow> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<Gallery> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
If I am correct that should be an optimal way from the point of data transferred as everything happens on the server side and you only get the results(images).
Just replace
public FollowingImages (Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery<ParseObject> whoDoIFollow = ParseQuery.getQuery("Follow")
.select("From")
.whereEqualTo("To", <user>);
ParseQuery<ParseObject> theirImages = ParseQuery.getQuery("Gallery")
.whereMatchesKeyInQuery("createdBy", "From", whoDoIFollow);
return theirImages;
}
});
}

How to save multiple parseObject with progresscallback

I am currently writing a program in which I need to upload multiple parseObjects like this:
List<ParseObject> listObjects;
ParseObject.saveAllInBackground(listObjects, new SaveCallback() {
#Override
public void done(ParseException e) {
}
}
But when the list increases, I need a way of notifying user the save progress is underway...
ParseFile has a built in
ParseFile.saveInBackground(SaveCallback saveCallback, ProgressCallback progressCallback)
My question is, how do I implement progresscallback using parseobject. If it's not possible, do I have to convert my parseobject to a parsefile?

Getting a ParseUser from a List<ParseUser> using Parse SDK for android?

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!

Categories

Resources