Skip to content

Instantly share code, notes, and snippets.

@stephanenicolas
Created September 26, 2017 23:12
Show Gist options
  • Save stephanenicolas/09d19130d6a4b9e572c533f18d6ebd50 to your computer and use it in GitHub Desktop.
Save stephanenicolas/09d19130d6a4b9e572c533f18d6ebd50 to your computer and use it in GitHub Desktop.
Good Old Way: Fragment
public class HeadlinesFragment extends ListFragment {
HeadlineListener mCallback;
// Container Activity must implement this interface
public interface HeadlineListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (HeadlineListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement HeadlineListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int p, long i) {
// Send the event to the host activity
mCallback.onArticleSelected(p);
}
}
@saguinav
Copy link

public class HeadlinesFragment extends ListFragment { There is a typo --> `HeadlineFragment

HeadlineListener mCallback; Hungarian notation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment