Reputation: 2256
I develop a message application using parse.com, there is a few class my data browser in parse com.
If class type custom, i can bind data's in column to my list view. Is class type User or Installation, i can't get my datas and bind to listview. I guess there is a security rule about this. well, how can i get values for "User" and "Installation" class type?
there is my codes, what can i add this?
@Override
protected Void doInBackground(Void... params) {
// Locate the class table named "User" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"User");
//query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into an ArrayAdapter
adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.listview_item);
// Retrieve object "name" from Parse.com database
for (ParseObject users : ob) {
adapter.add((String) users.get("username"));
}
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(MainActivity.this,
SingleItemView.class);
// Pass data "username" followed by the position
i.putExtra("username", ob.get(position).getString("username")
.toString());
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
Upvotes: 0
Views: 402
Reputation: 7108
The User objects are private, quick google search gave me this: https://parse.com/questions/how-can-i-find-parse-users-that-are-facebook-friends-with-the-current-user
Also you can only get your own installation object.
If you only need to list the users for the purpose of, say, Highscores, you could create a Highscore object containing:
username, score
When saving a score you can add a score column to you User or Parseinstallation depending on your needs. This way a user can easily get a reference to his own highscores. Make sure it has no ACL that makes it publically unreadable.
Now you are free to query the Highscores object.
Long answer short, factor out the things you wish to query in it's own object and add a reference to the user/installation.
Or perhaps even easier, the other way around, keeping a reference to the owner in the highscore:
username, score, owner
I am pretty confident that you are allowed to get the objectId of owner, whether it is user or installation. So a query like 'find Highscores where owner.objectId.equals(myUser.objectId)' should be valid.
Here is a snippet of my application. I have an option to save settings (sharedpreferences) in a parse.com object, thus sharing profiles between devices (parse is an object that I have implemented my self as a controller for all parse centric code, just not to be confused with anything from the parse library itself):
private void create() {
cat.removeAll();
switchPrefs = new ArrayList<SwitchPreference>();
currentProfile = parse.getParseProfile().getProfile();
parse.getParseProfile().getProfiles(new FindCallback<ParseObject>() {
@Override public void done(List<ParseObject> objects,
ParseException e) {
if (e == null) {
profiles = objects;
for (ParseObject profile : profiles) {
addProfile(profile);
}
}
}
});
}
private void addProfile(ParseObject profile) {
SwitchPreference switchPref = new SwitchPreference(getActivity());
switchPref.setTitle(profile.getString(ParseObjectHelper.Profile.name));
switchPref.setOrder(switchPrefs.size());
// switchPref.setKey(String.valueOf(profiles.size()));
if (currentProfile != null
&& profile.getObjectId().equals(currentProfile.getObjectId())) {
switchPref.setChecked(true);
} else {
switchPref.setChecked(false);
}
switchPref.setOnPreferenceChangeListener(new OnProfileChanged());
cat.addPreference(switchPref);
switchPrefs.add(switchPref);
if (!profiles.contains(profile)) {
profiles.add(profile);
}
}
So for each profile found attached to the current user adds a SwitchPreference.
Here is the code for getProfiles(..):
public void getProfiles(FindCallback<ParseObject> callback) {
ParseQuery<ParseObject> query = ParseQuery
.getQuery(ParseObjectHelper.Profile.objectname);
query.whereEqualTo(ParseObjectHelper.Profile.owner, parseUser);
query.findInBackground(callback);
}
To create a new Profile I do the following:
public void createProfile(final String profilename,
final GetCallback<ParseObject> callback) {
final ParseObject newProfile = new ParseObject(
ParseObjectHelper.Profile.objectname);
newProfile.put(ParseObjectHelper.Profile.name, profilename);
// the users ParseUser object
newProfile.put(ParseObjectHelper.Profile.owner, parseUser);
Map<String, ?> preferences = PreferenceManager
.getDefaultSharedPreferences(context).getAll();
for (String key : preferences.keySet()) {
if (key.startsWith("key")) {
newProfile.put(key, preferences.get(key));
}
}
newProfile.saveEventually(new SaveCallback() {
@Override public void done(ParseException e) {
callback.done(newProfile, e);
}
});
}
Upvotes: 1