How to do a relation in Parse Android? - android

I have some problems with relations in Parse, I don't know why I can't do a relation fine.
my app: a user can create posts, and look posts from other users, a user can add a favorite post to a list, and then see the list of their favorites. I think I need relations many to many for do it.
I have a button "add favorite" , a class "favorites" and a class "posts"
favorites has userId(current user) , and post(favorite post)
posts has namepost,description,category
I tried this.
ParseObject fav = ParseObject.create("favorites");
fav.put("userid",ParseUser.getCurrentUser());
ParseRelation relation = posts.getRelation("objectId");
fav.put("post",relation);
fav.saveInBackground();
But never saves . I also I tried to create a column in Parse "User" class but never works. Some suggestions?. Thanks.

Ok for answer my own question. I created a class Favorites with two columns, user, post
user: ParseUser.getCurrentUser()
post : obj.getobjectId
If I want to retrieve the favorites, first I search for them in a query and save it in a list.

Related

Saving Information Android

I am still fairly new to android and trying to learn day by day. I am trying to create a simple test application that you register for with an email/password to learn how to use save data from users.The app I am making is going to be simple, once they register and login, the user can search through a list of items I created on my MainActivity, and when they click on the listview item it will open a new activity. On the new activity, there will be a favorites button that I want to be able to click on and it will store that listview item information onto a favorites fragment I created and it will show the user a list of movies the user saved as favorites.
I am just trying to find out the best way I can do this. I have looked into SQLite and Firebase, hoping someone can explain to me which way would be the best way to approach this and maybe link me to a tutorial if possible.
Also the adding favorites to a listview item, if any one has a tutorial on that. I have already created my main listview and using Intents take the information and pass it to the new activity. It just clicking on the favorites button and saving the information into a new listview I do not know how to do, especially for when the users closes and re-opens the app the favorites will still be there.
Any help will be greatly appreciated.
To save favourite items in a new ListView you have to store the information somewhere. If you're using SQL, then store the Primary Key value in a favourites table that you can then use to find the corresponding information from the initial ListViews table.
If you are using firebase then each item in your list should have a unique key. Save that key and then when loading the favourites ListView, use the saved key to get the key's child data.
To summarise:
Store in a favourites table/branch the items the user selects as favourites and use that table/branch then to populate the favourites ListView.
I hope this helps
Another approach you could consider doing is storing your favorites using SharedPreference. The logic that would then follow is you have each row in your list view be an object that has an ID. Anytime a user favorites that object you save it into a set and store that set into SharedPreference that are corresponding to the current account username. You can then retrieve that set and show only the favorite objects for the user later on.
As for the SQLite vs Firebase question, I have not worked with Firebase yet, most of my interactions have been custom API calls. Having said that, I think SQLite would be a nice way to handle this as it would keep things easy and give you a good understanding of both sides of the equation.
Check out this tutorial. This very next one also covers SQLite:
https://www.youtube.com/watch?v=4SwAvFYMsXE

Android Back4App(Parse) get data from multiple classes

I am building an Android app using Back4App(Parse) database.
I have two classes Post and Post_likes. I want to fetch all posts listing from Post table with individual post likes from post_likes table in the same query.
In Post table, I have post details like post_id, user_id and other details.
In Post_likes table, I have user_id who liked the particular post corresponding to post_id.
Post table with post details
Post_likes table with Post_id and user_id
Now, I want to get total post likes of corresponding post and users who like the particular post along with post listing.
I want to fetch total likes of particular post and then show in home screen post listing.
So, how to write the query to combine the two tables.
Any help will be deeply appreciated.
To define a Pointer Column in the DB, From the Back4App's dashboard you can click on "Edit" button for one Class and add a new Column. Select the type "Pointer" and select the Class name you want to point to in the combo list.
In your code simple add the pointer in the corresponding field
(Something like:
ParseObject PostLikes aPostLike;
ParseObject Post thepost
aPostLike.put("pointedPost", thepost);
(See documentation for accurate syntax).
When you query List of PostLike you can add a "where clause" directly on the pointer Field "pointedPost".
You can also include the content of the Post while query all PostLikes with the key words "include":
(see doc here :
http://parseplatform.org/docs/android/guide/#relational-queries ) :
ParseQuery<ParseObject> query = ParseQuery.getQuery("PostLikes");
// Include the post data with each comment
query.include("post");

Android Parse query for data from more than two tables

I am using Parse for one of my android application and struggling for a query to get output.
Application Parse DB has three table which is as described below-
User: user related information with unique object Id
Post: Contains Post related information with unique object Id and UserId(Pointer to user table) who create the post
Like: UserId(Pointer to user table) represent who is Liking the post, PostId(Pointer to post table) which post is liking and unique
object Id
Now I want to make a single compound query for All post for a specific user along-with total number of likes to corresponding post. Please suggest a solution/query for that
When a user creates a post, assign the user objectID to some post identifier property in the post table.
To retrieve all posts from a user, a query like this may help:
query.whereEqualTo("objectID", currentUser.objectID)
If you're just concerned about the like count, rather than each user who has liked the post, you may want to have an Integer value in the post table that is incremented after a user presses the like button on that post.
You should create your own webHook.
Following this exemples here you can easily learn and develop your own stuff, you can even add some triggers to your application!
https://parse.com/docs/cloudcode/guide#cloud-code-cloud-functions
https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers
If User table doesn't have Post<.Relation> column
I suggest you to read about how to join queries.
f.g. make a query1 to get the user, then another query2 to get all posts with whereMatchesQuery('UserColumnInPostTable', query1) function.
Now, you are going to get all posts of a specific user efficiently.
.
If User table have Post<.Relation> column
this is the easiest way
Just get the user object and get the relation object, then get the query, and find in background

Android: ParseQueryAdapter to query multiple classes

This is my first question asked and hopefully someone can help me. I have looked around and nothing on this subject has helped me in my situation yet which is why I resorted to posting a request for help.
I have a parse.com database setup with the following Classes:
Users, Followers, and Items.
Followers contains two columns follower and following to keep track of which user is following which user.
Items contains an author column which is a pointer to a User object.
My goal is to make a query for a ParseQueryAdapter that will return Items objects. The path that I have to Items right now is to query the Followers class to get a list of users that the current user is following. Then use those user objects as pointers query the Items class for items the followed user has created.
Here is a diagram:
Followers
follower following
------- ---------
UserA UserB
UserB UserC
UserB UserA
Items
author column1 column2
------ ------- -------
UserA foo bar
UserA foo2 bar2
UserB blah blah
UserC blah blah
Based on the above database structure let's assume that currentUser is UserB. If UserB queries the Follower table he will retrieve his follower list (UserC and UserA) and then I would like to query to go out and retrieve all of the Items created by those that UserB follows (which should return 3 results based on the above structure). Based on my limited understanding of how parse.com works here is what I have tried:
public class ItemListItemAdapter extends ParseQueryAdapter<ParseObject>{
public ItemListItemAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseUser currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> followerQuery = new ParseQuery("Followers");
followerQuery.whereEqualTo("follower", currentUser);
ParseQuery<ParseObject> itemQuery = new ParseQuery("Items");
itemQuery.whereMatchesQuery("author", followerQuery);
return itemQuery;
}
});
}
As I understand it I am first creating a query that will get the followers that currentUser is following. Then I should have a list of User objects that can be used to query the Items class where the User object matches the followers so that I can populate my ListView item with the right data from that Item object but this query returns zero results.
So either I am modeling everything wrong or I am misunderstanding how relationships work. Any help would be greatly appreciated.
If I remove the itemQuery and just return the followerQuery then it works - the problem is with the inner query when I try to combine the two (or it could be me complicating my life with the data structure in the database so I'm also open to suggestions on restructuring).
Thanks for your help in advance!
The answer was to not use whereMatchesQuery() but whereMatchesKeyInQuery() instead.
here is an example using whereMatchesKeyInQuery() (that is not necessarily specific to this question but demonstrating the use of whereMatchesKeyInQuery by request in the comments):
ParseQuery followerQuery = ParseQuery.getQuery("Followers");
followerQuery.whereEqualTo("following", ParseUser.getCurrentUser());
userQuery.whereMatchesKeyInQuery("objectId", "followerId", followerQuery);
So the parameters would be:
query1.whereMatchesKeyInQuery("key_value_in_query1", "key_value_to_match_in_query2", query2);

Retrieving Database Information and Showing in ListView with Android

I am working on an Android app that will allow the user to see restaurants in the city I live in. I am storing each restaurants information (name, address, telephone, hours, category, website) in an SQLite database.
What I am trying to do is to create a SortByAlpha activity that will list the restaurants by name in alphetically-descending order.
I understand that I should be using a Cursor to do this but I can't find a half decent tutorial, all of the "tutorials" I find are a bunch of code with minimal explanation. How can I do this / Where can I find a good tutorial?
Use SimpleCursorAdapter, which bridges between Cursor and Adapter. Here is an example how to use it: http://developer.android.com/guide/topics/ui/binding.html
I would advise creating a "Restaurant" class that contains all the fields you will be listing. Then make your SQL call and specify "ORDER BY Name". Then create an ArrayList that can be fed to your custom list adapter! Also, if they were to somehow get out of order, just implement the "Comparable" interface for the Restaurant class and compare based on "Name" and then you can call Collections.sort(restaurantlist); to sort them in alphabetical order. I personally think ORDER BY is the easier way to go!

Categories

Resources