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

Android Build a Self-Destructing Message Android App Sending Messages Selecting Recipients

Juan Francisco Andrade Álvarez
Juan Francisco Andrade Álvarez
23,997 Points

Refactoring common code in RecipientsActivity and FriendsFragment: trying to accomplish DRY

As this video shows, code has been copied and pasted from FriendsFragment to the RecipientsActivity. I think that some separation of concerns could be made here. In fact, i've created a class FriendsChooser which can be found here: https://gist.github.com/franc014/254075a49d3ae7bffc68

This class could be instantiated in whichever view needs it. For example in the RecipientsActivity class, the onCreate method would change to:

super.onResume();
FriendsChooser friendsChooser = new FriendsChooser(mContext,mProgressBar);
friendsChooser.friends();
if(!friendsChooser.getError()) {
   List<String> friends =  friendsChooser.getUserNames();
   ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
   android.R.layout.simple_list_item_checked, friends);
   setListAdapter(adapter);
}else{
   RibbitAlert alert = new RibbitAlert();
   alert.setTitle(getString(R.string.error_alert_title));
   alert.setMessage(getString(R.string.no_friends_message));
   alert.show(getFragmentManager(),"friends_error");
}

However, i can't see this work. It fails to pass the 'usernames' array from the 'findInBackground' callback in FriendsChooser to the 'friends' list in the RecipientsActivity's onCreate method; it's just null.

Is there a way to pass the usernames list or array or any other data from the Parse callback, so that there is any kind of separation of concerns (data in the Friends Chooser, presentation in the fragments or activities)?

If there was any other way to make this parts of the app DRY, any ideas are well appreciated.

Thanks,