Parse: +1 to a number column for rating - android

My Android app has a very simple love it or hate it rating system. I am using Parse for the ratings.
All is fine in terms of querying Parse for number of ratings and also putting new ratings into parse.
The problem I'm having is:
I have not found a way on Parse to +1 to a number column, so what I have been doing is getting the current value of the rating, +1 to that number and then using Parse's Put method to write it back.
This seems ok but if 2 users open the app 1 second after the other they both see the rating is 8, then both click +1, it will get the value of the rating (8) for user A, +1 to 8 = 9 and then write 9 back.
1 second later user B does the same thing, they still have the value of 8 stored on their device so theirs will also be 8 +1 = 9, where it should have been 10.
So my question is: Is there a way to +1 to a number column in Parse?
If not I'll add the getRatings method before a user rates so it should get the latest value but was just hoping to reduce bandwidth.
Thanks.
Adam
public void getRatings() {
//query to get love it/hate it from Parse
//we are only quering to get the first in background because we know there is only
//one we want
ParseQuery<ParseObject> query = ParseQuery.getQuery("ratings");
query.whereEqualTo("Id", id);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException error) {
if (object != null) {
//if the show has been rated before
loveIt = object.getInt("loveIt");
hateIt = object.getInt("hateIt");
retreivedObject = object;
parseId = object.getObjectId();
//hide progress bar
mProgressBar.setVisibility(View.INVISIBLE);
}
else
{
//if the show has not been rated before
loveIt=0;
hateIt=0;
//hide progress bar
mProgressBar.setVisibility(View.INVISIBLE);
}
}
});
}

You need to use a counter. See official documentation below.
https://www.parse.com/docs/android_guide#objects-updating

Related

get last arraylist position before streak gets interrupted

https://www.google.com/search?q=message+image+on+last&sxsrf=ALeKk01qNrLQ61yRXCMSPRxyNaoI0MF_kA:1605295893515&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjj2veboYDtAhWeIbcAHebnB4QQ_AUoAXoECAwQAw&biw=1366&bih=657#imgrc=wzgnznL_g7kUsM
I want to show profile image like shown in above link only at last message of every consecutive messages from same person. Please help me with this.
I have tried many ways but I don't know to hide visibility of top images
if (position > 0) {
if (messageList.get(i).getFrom.equalsIgnoreCase(messagesList.get(position - 1).getFrom()))
{
viewHolder.mProfileImage.setVisibility(View.INVISIBLE);
}
else
{
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
}
else
{
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
I have used above code and get this result which is shown in this http://i.stack.imgur.com/92SUb.jpg link. With this code I am able to hide all the images other than the first one image. can you please show me the way to hide top message image and not the last one of streak.
I have to assume few things in my answer.
i.e.
messageList is the list of all messages. not the list of only consecutive/streak messages.
you need to show profile images for both sender and receiver.
And you want to only show the profileImage of last consecutive message.
So, here what you should do.
int length = messageList.lenght; //getTotalLenght;
String fromCurrent = messageList[position].getFrom(); //getFrom of current message being painted or shown
String fromNext = fromCurrent; //next message is set to current by default, to avoid error
if(length > postition+1) {
fromNext = messageList[position+1].getFrom(); //if there is next message then get its sender but if it is the last message then nextSender and currentSender are same as I set its default value to the currentSender.
}
console.log("TAG", "FromNext: " + fromNext);
console.log("TAG", "FromCurrent: " + fromCurrent);
if(!fromCurrent.equals(fromNext)) { //checking if last message or not
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
else {
viewHolder.mProfileImage.setVisibility(View.INVISIBLE);
}

How can i sort the ListView according to the phone number? [duplicate]

This question already has answers here:
Android-java- How to sort a list of objects by a certain value within the object
(15 answers)
Closed 4 years ago.
I have a list of user with phone number. I have one menu to sort the list according to phone number? I tried using the Collection.sort() method but is is not working as i expected.
The json respose of user is :-
{
"results":[
{
"name":{
"title":"mrs",
"first":"hayley",
"last":"moore"
},
"email":"hayley.moore#example.com",
"dob":{
"date":"1965-10-21T18:13:17Z",
"age":52
},
"phone":"(852)-247-7958",
"picture":{
"large":"https://randomuser.me/api/portraits/women/26.jpg",
"medium":"https://randomuser.me/api/portraits/med/women/26.jpg",
"thumbnail":"https://randomuser.me/api/portraits/thumb/women/26.jpg"
}
},
{},
.
.
]
}
Use this code for sort your list on the criteria of Phone Number.
Put your model in place of user.
put your field getter in place of getPhoneNum.
Collections.sort(users, new Comparator<User>() {
#Override
public int compare(User u1, User u2) {
return u1.getValue().replace("(","").compareTo(u2.getValue().replace("(",""));
}
});

How can I get the list of Open Channels more than 1000 in android using the SendBird SDK?

I'm developing the chatting application using the SendBird SDK in android studio.
How can I get the list of Open Channels more than 1000?
You can create use an OpenChannelListQuery to traverse through your list of channels in fixed chunks.
OpenChannelListQuery query = OpenChannel.createOpenChannelListQuery();
query.setLimit(30);
query.next(new OpenChannelListQuery.OpenChannelListQueryResultHandler() {
#Override
public void onResult(List<OpenChannel> list, SendBirdException e) {
if (e != null) {
// Error!
}
// list contains the first 30 channels.
}
});
As long as your query instance is the same, you can call query.next() until you obtain as many channels as you want.
query.next(new OpenChannelListQuery.OpenChannelListQueryResultHandler() {
#Override
public void onResult(List<OpenChannel> list, SendBirdException e) {
// list contains the next 30 channels.
}
});
Edit: I forgot to mention that the first query.next() must be completely finished before you can call query.next() again. That is, make sure the first onResult() is called before calling query.next() again.

Random behavior in Android Studio

I'm working in an app using Parse.com and I've been experiencing some random behavior, I made a simpler version of my code to show you and while it keeps being random (the results I expect happen about 4 in 5 times) it got better (in my code it's correct about 2/5 of the runs).
So the method is this, a very simple creation and filling of a new object:
public void test(){
final List<ParseObject> list = new ArrayList<>();
for(j=0;j<4;j++){
list.add(new ParseObject("Object"));
list.get(j).put("Column1", "sup");
list.get(j).put("Column2", "bro");
if(j==3){
ParseObject.saveAllInBackground(list, new SaveCallback() {
#Override
public void done(ParseException e) {
Toast.makeText(MainActivity.this, "NANANANANANANANA BATMAN!",Toast.LENGTH_LONG).show();
}
});
}
}
}
}
Problem is it creates 4 ParseObjects (as it should) only 4/5 of the tries, then 1/4 it's only 3. Why does this happen?
Why don't you create and store your ParseObject in a variable and then, use put("Column1", "sup"); and put("Column2", "bro"); on it before adding this variable directly to your list? because you are accessing your list two times for nothing where it can be avoided.
It would be cleaner and it would allow you to debug better.

I want to set a calculated priority using the server timestamp provided by Firebase

I would like to set the priority of a child using the server time stamp provided by Firebase, ServerValue.TIMESTAMP:
mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP);
But my case is inverse. I want to set negative ServerValue.TIMESTAMP to move my child to the top based on time. Is it possible to do that in Firebase without using the local time stamp System.CurrentTimeInMillis()?
I would like to do something like this:
mFirebaseref.child(userid).setPriority(-ServerValue.TIMESTAMP);
On the client side, ServerValue.TIMESTAMP is an object structured like this: {.sv: "timestamp"}
So, as you know, you can't easily do what you wanted. However, there may be a different solution. If, for example, you wanted the five most recent entries, you could still set the priority by ServerValue.TIMESTAMP:
mFirebaseref.child(userid).setPriority(ServerValue.TIMESTAMP);
And then use the limitToLast() method:
Query queryRef = mFirebaseref.limitToLast(5);
To get the five most recent entries.
Also, this may help: Display posts in descending posted order
You are basically asking how to get negative server timestamp and it should work offline. I found a way, there is a hidden field you can use. A snippet from documentation:
Firebase offsetRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/serverTimeOffset");
offsetRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
double offset = snapshot.getValue(Double.class);
double estimatedServerTimeMs = System.currentTimeMillis() + offset;
}
#Override
public void onCancelled(FirebaseError error) {
System.err.println("Listener was cancelled");
}
});

Categories

Resources