Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,