Loyal Loyal - 1 month ago 10
Android Question

Check if Parse Array contain value android

I use Parse in android. I am trying to check if an array contain an value (ObjectId).

private void getCurrentObject() {
query = ParseQuery.getQuery("Group");
query.getInBackground(objectid, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
object = all;
if (e == null) {
if (userexists(object.getJSONArray("members_array"), currentUser.getObjectId())) {
Toast.makeText(GroupActivity.this, "isDo", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(GroupActivity.this, "isNot", Toast.LENGTH_SHORT).show();
}
}
}
});
}

private boolean userexists(JSONArray jsonArray, String usernameToFind) {
return jsonArray.toString().contains(usernameToFind);

}


Thank you in advanced

Answer

Remove this line

object = all;

Or, you probably meant all = object

Then, you need to loop over the JSONArray, not toString it.

private boolean userexists(JSONArray jsonArray, String usernameToFind) {
    if (jsonArray == null) return false;

    for (int i = 0; i < jsonArray.length(); i++) {
        String s = jsonArray.optString(i, "");
        if (s.equals(usernameToFind)) return true;
    }

    return false;
}

Using this method

String optString(int index, String fallback)
Returns the value at index if it exists, coercing it if necessary.

Comments