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
Justin Ezor
2,193 PointsWhy don't Checkmarks stay on when the Edit Friends List is opened from Options Menu?
I'm following along with the Build a Self-Destructing message Android app track right now and I've come to a road block. I cannot get my Edit Friends Activity to display check marks when I open it, which can only mean that I'm either not saving the relation or I'm missing something.
Here's my Edit Friends Activity code:
'''public class EditFriendsActivity extends ListActivity {
public static final String TAG = EditFriendsActivity.class.getSimpleName();
protected List<ParseUser> mUsers;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_edit_friends);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_friends, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success
mUsers = users;
String[] usernames = new String[mUsers.size()];
int i = 0;
for(ParseUser user : mUsers) {
usernames[i] = user.getUsername();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
EditFriendsActivity.this,
android.R.layout.simple_list_item_checked,
usernames);
setListAdapter(adapter);
addFriendCheckmarks();
}
else {
Log.e(TAG, e.getMessage());
AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendsActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (getListView().isItemChecked(position)) {
// remove the friend
}
else {
// add the friend
mFriendsRelation.add(mUsers.get(position));
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.e(TAG, e.getMessage());
}
}
});
}
}
private void addFriendCheckmarks() {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
//list returned - look for a match
for (int i = 0; i < mUsers.size(); i++) {
ParseUser user = mUsers.get(i);
for (ParseUser friend : friends) {
if (friend.getObjectId().equals(user.getObjectId())) {
getListView().setItemChecked(i, true);
}
}
}
}
else {
Log.e(TAG, e.getMessage());
}
}
});
}
}
'''
Any help would be greatly appreciated!
2 Answers
Evelina Yankova
5,869 Pointswell...in the else-block you'll put the code that will execute in case the user is not checked. In other words you will remove the relation between the logged user and the unchecked friend(it is somewhere in the next video I think - Removing Friend Relationships). I tried your code and it worked for me. I only moved this in the if-block of the OnListItemClick method and left the else-block empty.
if(getListView().isItemChecked(position)){ mFriendsRelation.add(mUsers.get(position)); mCurrentUser.saveInBackground(new SaveCallback() { @Override public void done(ParseException e) { if (e != null) { Log.e(TAG, e.getMessage()); } } }); } Do you get the same error as the one you posted above?
Evelina Yankova
5,869 PointsHi, did you manage to make the app work? Check your OnListItemClick method and the if-else statement. Your friends should be added in the if part, when checking the getListView().isItemChecked(position). Move the: mFriendsRelation.add(mUsers.get(position)); mCurrentUser.saveInBackground(new SaveCallback()
in the if part
Justin Ezor
2,193 PointsWhen I try that it Ribbit closes when I tap a check mark.
Should I have something in the Else Block?
Justin Ezor
2,193 PointsJustin Ezor
2,193 PointsAlso, when I open the Edit Friends Activity on my Android device and try to toggle the Checkmark on and off for a user the activity closes and I am sent back to main activity with the error message "Unfortunately, Ribbit has stopped." In the LogCat I get this error:
"08-21 10:16:10.697: E/AndroidRuntime(12264): FATAL EXCEPTION: main 08-21 10:16:10.697: E/AndroidRuntime(12264): Process: com.teamtreehouse.ribbit, PID: 12264 08-21 10:16:10.697: E/AndroidRuntime(12264): java.lang.IllegalArgumentException: Related object object must be of class _Role, but _User was passed in. 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.parse.ParseRelationOperation.apply(ParseRelationOperation.java:228) 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.parse.ParseObject.performOperation(ParseObject.java:2471) 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.parse.ParseRelation.add(ParseRelation.java:87) 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.teamtreehouse.ribbit.EditFriendsActivity.onListItemClick(EditFriendsActivity.java:117)
I think this is critical info^^ "IllegalArgumentException: Related object object must be of class _Role, but _User was passed in."
08-21 10:16:10.697: E/AndroidRuntime(12264): at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.widget.AdapterView.performItemClick(AdapterView.java:299) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.widget.AbsListView$3.run(AbsListView.java:3645) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.os.Handler.handleCallback(Handler.java:733) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.os.Handler.dispatchMessage(Handler.java:95) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.os.Looper.loop(Looper.java:136) 08-21 10:16:10.697: E/AndroidRuntime(12264): at android.app.ActivityThread.main(ActivityThread.java:5086) 08-21 10:16:10.697: E/AndroidRuntime(12264): at java.lang.reflect.Method.invokeNative(Native Method) 08-21 10:16:10.697: E/AndroidRuntime(12264): at java.lang.reflect.Method.invoke(Method.java:515) 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 08-21 10:16:10.697: E/AndroidRuntime(12264): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 08-21 10:16:10.697: E/AndroidRuntime(12264): at dalvik.system.NativeStart.main(Native Method)