I am trying to parse a boolean value after using it with parseObject, but I cannot parse it?
Here is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
happend = (boolean) smsObject.get("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You should call default methods of ParseObject to retrieve data instead of parsing :
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
if (smsObject!=null) {
Date time = smsObject.getDate("date");
myMsg = smsObject.getString("message");
usrNum = smsObject.getString("phoneNumber");
happend = smsObject.getBoolean("happend");
result = time;
}
if (result != null) {
if (System.currentTimeMillis() >= result.getTime() && happend == false) {
// count++;
if (usrNum != null && myMsg != null) {
Log.d("message", myMsg);
Log.d("time", String.valueOf(result));
sendMsg2(myMsg, usrNum);
smsObject.put("happend", true);
}
}
}
}
}
}
});
You can simply use For loop for getting single object of Parse object and can use getBoolean() method for getting boolean value.
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
happend = objects.get(i).getBoolean("happend");
}
}
}
Related
If you are querying users in a 100km radius how do you exclude yourself (the current user) from the query?
Because when onDocumentMoved triggers if you are the only one moving you trigger on document entered you will get the users again but with different distance between you and the users, thus you will get duplicate results if you are adding them to the database in the onDocumentEntered method. The way I solved it is deleting all the usersInRange from the database when the app is opened again. I know this is not the perfect solution and I am seeking a better one.
Here is my code that does this:
public void getLocation(Location location){
ArrayList<String> docMoved = new ArrayList<>();
listenerRegistration = documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot value, #Nullable FirebaseFirestoreException error) {
if(value != null && value.exists()){
if (value.get("LocationRange") != null && value.get("AgeRangeFrom") != null && value.get("AgeRangeTo") != null && value.getString("InterestInGender") != null) {
locationRange = (double) value.get("LocationRange");
ageRangeFrom = (double) value.get("AgeRangeFrom");
ageRangeTo = (double) value.get("AgeRangeTo");
interestInGender = value.getString("InterestInGender");
ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) value.get("UsersInRange");
geoQuery = geoFirestore.queryAtLocation(new GeoPoint(location.getLatitude(), location.getLongitude()), locationRange);
geoQuery.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
#Override
public void onDocumentEntered(#NotNull DocumentSnapshot documentSnapshot5, #NotNull GeoPoint geoPoint) {
if (documentSnapshot5.get("age") != null && documentSnapshot5.getString("Gender") != null && documentSnapshot5.get("Images") != null && !documentSnapshot5.getId().equals(user.getUid())) {
age = (long) documentSnapshot5.get("age");
userGender = documentSnapshot5.getString("Gender");
userPhotos = (ArrayList<String>) documentSnapshot5.get("Images");
Location locationNew = new Location(documentSnapshot5.getId());
locationNew.setLatitude(geoPoint.getLatitude());
locationNew.setLongitude(geoPoint.getLongitude());
distanceM = location.distanceTo(locationNew);
distanceKM = (int) Math.round(distanceM / 1000);
if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Women") && userGender.equals("Woman")) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
} else if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Men") && userGender.equals("Man")) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
} else if (age >= ageRangeFrom && age <= ageRangeTo && interestInGender.equals("Everyone")) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayUnion(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
} else {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(new User(documentSnapshot5.getString("Name"), age, userPhotos.get(0), documentSnapshot5.getId(), distanceKM, null)));
}
}
}
#Override
public void onDocumentExited(#NotNull DocumentSnapshot documentSnapshot1) {
db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
if (usersInRange != null && !usersInRange.isEmpty()) {
for (int i = 0; i < usersInRange.size(); i++) {
if (usersInRange.get(i).containsValue(documentSnapshot1.getId())) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
}
}
}
}
});
}
#Override
public void onDocumentMoved(#NotNull DocumentSnapshot documentSnapshot6, #NotNull GeoPoint geoPoint) {
docMoved.add(documentSnapshot6.getId());
if(!documentSnapshot6.getId().equals(user.getUid())) {
db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
if (usersInRange != null && !usersInRange.isEmpty()) {
for (int i = 0; i < usersInRange.size(); i++) {
if (usersInRange.get(i).containsValue(documentSnapshot6.getId())) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
}
}
}
}
});
}
}
#Override
public void onDocumentChanged(#NotNull DocumentSnapshot documentSnapshot1, #NotNull GeoPoint geoPoint) {
if(docMoved.size() == 1 && docMoved.get(0).equals(user.getUid())){
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.delete());
}
db.collection("UsersFilter").document(user.getUid()).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
ArrayList<HashMap<Object, Object>> usersInRange = (ArrayList<HashMap<Object, Object>>) documentSnapshot.get("UsersInRange");
if (usersInRange != null && !usersInRange.isEmpty()) {
for (int i = 0; i < usersInRange.size(); i++) {
if (usersInRange.get(i).containsValue(documentSnapshot1.getId())) {
db.collection("UsersFilter").document(user.getUid()).update("UsersInRange", FieldValue.arrayRemove(usersInRange.get(i)));
}
}
}
}
});
}
#Override
public void onGeoQueryReady() {
}
#Override
public void onGeoQueryError(#NotNull Exception e) {
}
});
}
}
}
});
This is the code that displays the users in a ViewPager2:
listenerRegistration = documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
#Override
public void onEvent(#Nullable DocumentSnapshot value, #Nullable FirebaseFirestoreException error) {
if(value != null && value.exists()) {
usersInRange = (ArrayList<HashMap<Object, Object>>) value.get("UsersInRange");
usersInChat = (ArrayList<HashMap<Object, Object>>) value.get("UsersInChat");
imageUrls.clear();
viewPager2Adapter.notifyDataSetChanged();
if (usersInChat != null && !usersInChat.isEmpty() && usersInRange != null && !usersInRange.isEmpty()) {
for (int i = 0; i < usersInChat.size(); i++) {
for (int k = 0; k < usersInRange.size(); k++) {
if (usersInRange.get(k).containsValue(usersInChat.get(i).get("userID"))) {
usersInRange.remove(usersInRange.get(k));
}
}
}
}
if (usersInRange != null && !usersInRange.isEmpty()) {
for (int i = 0; i < usersInRange.size(); i++) {
String profileImage = (String) usersInRange.get(i).get("profilePic");
String userName = (String) usersInRange.get(i).get("name");
long userAge = (long) usersInRange.get(i).get("age");
String userID = (String) usersInRange.get(i).get("userID");
long km = (long) usersInRange.get(i).get("km");
imageUrls.add(new User(userName, userAge, profileImage, userID, km, null));
viewPager2Adapter.notifyDataSetChanged();
}
}
}
}
});
public void pictureQuery() {
friendsPic = new ArrayList<>();
for (int i = 0; i < friendsList.length(); i++) {
ParseQuery parseQuery = ParseQuery.getQuery("Data");
parseQuery.whereEqualTo("firstName", "John");
//parseQuery.whereEqualTo("userId",friendsID.get(i));
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
String link = objects.get(0).get("picUrl").toString();
Bitmap bitmap = null;
try {
bitmap = task.execute(link).get();
friendsPic.add(bitmap);
} catch (Exception e1) {
e1.printStackTrace();
}
Log.i("FDA, done", friendsPic.toString());
} else {
Log.i("FD", "0002");
}
} else {
e.printStackTrace();
}
}
});
}
}
Hi guys,
I am trying to retrieve picture URL from parse server (stored as a string), originally I was trying to make a query using the Id, but also tried to hard code it using:
parseQuery.whereEqualTo("firstName", "John");
It always seems to go to the else statement of object.size() > 0 i.e.
Log.i("FD", "0002");
I think i've checked everything including:
size of friendsList ( for the for loop )
spelling, made sure that "Data", "firstName" and "John" are the same on Parse Server
The value of friendsID.get(i)
Tried to use findFirstInBackground instead (same issue)
I have also compared it with my other code for different data ( that works ) :
ParseQuery parseQuery = ParseQuery.getQuery("SportData");
parseQuery.whereEqualTo("id", mApp.getId());
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
saveSportData(objects.get(0), false);
} else if (objects.size() < 1) {
saveSportData(null, true);
}
} else {
e.printStackTrace();
}
}
});
Any idea what am I doing wrong, or what may be causing it ?
Thanks a lot.
Retrieved data and stored in local database but while trying to pull relation from the local database I get empty list which indicates that the relation data is not pinned to the local database.
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);
}
}
});
}
}
});
So how should I pin "relation" data to the local database ???
What is the best way to read comments which is a relation. shall I store them in local datastore or pull data in the background ??
make sure you enableLocalDatastore store in application class Parse.enableLocalDatastore(getApplicationContext());
Parse local data store will not work with cache policy. We can access relational data like below
private void getParseData() {
if (parseObjectId != null) {
mApp.show_PDialog(context, "Loading..");
ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Profile");
parseQuery.getInBackground(parseObjectId, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject parseObject, ParseException e) {
if (e == null) {
try {
newName = parseObject.getString("name");
newGender = parseObject.getString("gender");
newDOB.setTime(parseObject.getDate("dob"));
newTOB.setTime(parseObject.getDate("tob"));
newCurrentLocation = parseObject.getParseObject("currentLocation");
newPOB = parseObject.getParseObject("placeOfBirth");
if (newName != null) {
displayName.setText(newName);
}
if (newGender != null) {
gender.setText(newGender);
}
if (newDOB != null) {
DateFormat df = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String subdateStr = df.format(newDOB.getTime());
datePicker.setText(subdateStr);
}
if (newTOB != null) {
DateFormat df = new SimpleDateFormat("hh:mm a", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String subdateStr = df.format(newTOB.getTime());
timepicker.setText(subdateStr);
}
if (newPOB != null) {
placeOfBirth.setText(newPOB.fetchIfNeeded().getString("name")
+ ", " + newPOB.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name") + ", " + newPOB.getParseObject("Parent").fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name"));
}
if (newCurrentLocation != null) {
currentLocation.setText(newCurrentLocation.fetchIfNeeded().getString("name")
+ ", " + newCurrentLocation.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name") + ", " + newCurrentLocation.fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getParseObject("Parent").fetchIfNeeded().getString("name"));
}
} catch (ParseException e1) {
e1.printStackTrace();
}
} else {
e.printStackTrace();
}
mApp.dialog.dismiss();
}
});
} else {
getActivity().finish();
}
}
I'm trying to read from parseObject. I have a class over there and I'm trying to get the messages from there but I get only the last message that is sent from the device.
So if I send two messages only the last one is sent.
Can anyone help me?
This is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
}
} else {
}
}
});
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
Use this
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
}
I hope this will solve your problem
It seems that myMsg and usrNum are not an Array or a List.
You only call sendMsg2(myMsg, usrNum) once, after the loop has ended. That is why you only get the last message.
Move sendMsg2(myMsg, usrNum) into the loop.
The problem seems to be that you have located your sendMsg2() outside of both - the callback scope and the loop scope. If you want to have all of your messages being sent you have to put sendMsg2() inside of the loop.
ParseQuery<ParseObject> query = ParseQuery.getQuery("SmsTable");
query.whereEqualTo("deviceId", android_id);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, com.parse.ParseException e) {
if (e == null) {
for (ParseObject smsObject : objects) {
Date time = (Date) smsObject.get("date");
myMsg = (String) smsObject.get("message");
usrNum = (String) smsObject.get("phoneNumber");
//happend = (boolean) smsObject.get("happend");
result = time;
if (System.currentTimeMillis() >= result.getTime()&& happend == false) {
// count++;
sendMsg2(myMsg, usrNum);
happend = true;
}
}
} else {
}
}
});
I have 37 files to download and I wanted to show my progress bar complete in 100%. how can I set that my last file will equal to 100%.. sorry for my english. thanks in advance..
querySound.findInBackground(new FindCallback<ParseObject>() {
#SuppressWarnings("unused")
#Override
public void done(List<ParseObject> objects, ParseException e) {
Log.d("OBJECTS MP3", ""+objects.toString());
if (objects == null) {
Toast.makeText(ParseStarterProjectActivity.this,"Something is Wrong!", Toast.LENGTH_LONG).show();
} else {
final int adder = 100 / objects.size();
for(ParseObject po : objects){
ParseFile sounds = (ParseFile) po.get("remindersSpanish");
final String filenames = sounds.getName().substring(37, sounds.getName().length());
Log.d("OBJECTS NAME", filenames);
sounds.getDataInBackground(new GetDataCallback() {
#Override
public void done(byte[] data,ParseException e) {
Log.d("OBJECTS DATA", ""+data);
if (e == null) {
saveMp3(filenames, data);
counter = counter + adder;
} else {
e.printStackTrace();
}
prgBar.setProgress(counter);
counterPrg.setText(counter+"/"+prgBar.getMax());
}
});