Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Richard Luick
10,955 PointsGetting Parse Object ID from onIListItemClick
Hello, I am currently working on the extra credit "User Profiles: Do we really know our "Friends"?" for the Ribbit app. When the user clicks on a friend in the friends list, I would like them to be directed to a profile page for this friend. To do this, I have been trying to extract the specific object id from the user that is clicked on and then pass that onto the next activity where I use it to run a query for all the users other attributes (name, email, hometown, etc.). I have figured out how to pass along data through intents but I am having all sorts of trouble obtaining the user id from the list item clicked. My onItemCLick code is below.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ParseObject item = (ParseObject) l.getAdapter().getItem(position);
String objectID = item.getObjectId().toString();
Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
intent.putExtra("ID", objectID);
startActivity(intent);
}
I would think that the getObjectId method would work but my app crashes as soon as I click on a friend. Is there something wrong with my getItem(position) method or something like that?
Thanks!
4 Answers
Richard Luick
10,955 PointsAlright so I was able to figure it out. I started by creating a String ArrayList to store the Objects Ids:
protected ArrayList<String> mObjectIds = new ArrayList<String>();
Then when storing the usernames in the array for the List Adapter, I passed the objects Ids into the ArrayList:
for (ParseUser user : mFriends) {
usernames[i] = user.getUsername();
mObjectIds.add(user.getObjectId());
i++;
}
Then on the listItemClick I extracted the objectId of the item clicked and passed it along to the new Activity:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String itemID = mObjectIds.get(position);
Intent intent = new Intent(getActivity(), FriendsProfileActivity.class);
intent.putExtra("ID", itemID);
startActivity(intent);
}
More info along with other ways to go about this can be found here: http://stackoverflow.com/questions/25574243/getting-parse-object-id-from-onlistitemclick/25574982?noredirect=1#comment40041216_25574982
Milan Tailor
5,132 PointsThis one's been doing my head in all night, luckily came across this- you are a smarter man than me. I've had a look at the stack overflow link, but am still a bit perplexed how you got there. Any tips or methods you used to get to the answer (I mean i understand now, seeing it all done- but even if I spent few more hours on it myself, I doubt I'd be anywhere close).
Thanks!
Milan Tailor
5,132 PointsI'm sure this thread's dead, but how did you manage to pass the extra details (location etc.) to the FriendsProfile view? Did you just send the Id and get the rest from that or carry them in the intent, either way any hints into how you did it?
Richard Luick
10,955 PointsHello Milan, this thread is a bit old so Im not fully in tune with what I was doing at the time. But you are correct. I passed along the objects parse Id to the Friends Profile and then ran a query on that object Id to get the rest of the data.
Richard Luick
10,955 PointsThis is the entire onCreate method from the profily activity. Once you pass the id over as an extra, you can use it to run a query and get the other fields from that object.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends_profile);
mUsernameField = (TextView) findViewById(R.id.usernameSpace);
mFullNameField = (TextView) findViewById(R.id.fullNameSpace);
mEmailField = (TextView) findViewById(R.id.emailSpace);
mHometownField = (TextView) findViewById(R.id.hometownSpace);
mWebsiteField = (TextView) findViewById(R.id.websiteSpace);
mId = getIntent().getStringExtra(ParseConstants.KEY_ID);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.getInBackground(mId, new GetCallback<ParseUser>() {
@Override
public void done(ParseUser parseUser, com.parse.ParseException e) {
if (e == null) {
mUsername = parseUser.getUsername();
mFirstName = parseUser.get(ParseConstants.KEY_FIRST_NAME).toString();
mLastName = parseUser.get(ParseConstants.KEY_LAST_NAME).toString();
mFullName = mFirstName + " " + mLastName;
mEmail = parseUser.getEmail();
mHometown = parseUser.get(ParseConstants.KEY_HOMETOWN).toString();
mWebsite = parseUser.get(ParseConstants.KEY_WEBSITE).toString();
mUsernameField.setText(mUsername);
mFullNameField.setText("Name: " + mFullName);
mEmailField.setText("Email: " + mEmail);
mHometownField.setText("Hometown: " + mHometown);
mWebsiteField.setText("Website: " + mWebsite);
Linkify.addLinks(mWebsiteField, Linkify.ALL);
}
else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(FriendsProfileActivity.this);
builder.setTitle(R.string.error_title)
.setMessage(e.getMessage())
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
Milan Tailor
5,132 PointsTrue gent! Much appreciated, I was trying all sorts of nonsense, when all I needed was the ParseContacts.
Charles L. Kirkwood
Courses Plus Student 1,140 PointsHi Richard, I'm trying to direct the user to the FriendsProfileActivity using the onListItemClick but I cannot seem to get it right. Can you please paste here your full FriendsFragement activity so I can have a reference?
Thanks!
Richard Luick
10,955 PointsRichard Luick
10,955 PointsThis is the logcat