I have this kind of Java source code with a Listener.
public static class FragmentA extends ListFragment {
OnArticleSelectedListener mListener;
...
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(int position);
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
"Activity must implement OnArticleSelectedListener");
}
}
...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mListener.onArticleSelected(position);
}
}
And I implement it in this way:
public class MyActivity implements FragmentA.OnArticleSelectedListener {
...
@Override
public void onArticleSelected(int pos) {
// do something
}
}
So, now I want to transform it to Scala. I think, I should use traits. [SOLVED: In this class I get the error in onAttach
that not found: value OnArticleSelectedListener
]:
class FragmentA extends ListFragment {
var mListener: OnArticleSelectedListener = null
...
trait OnArticleSelectedListener {
def onArticleSelected(position: Int)
}
...
override def onAttach(activity: Activity) {
super.onAttach(activity)
mListener = (OnArticleSelectedListener) activity;
}
...
override def onListItemClick(l: ListView, v: View, position: Int, id: Long) {
// Send the event to the host activity
mListener.onArticleSelected(position);
}
}
In the Activity, I do not know how to "implement" the trait. I know the keyword is "with", but extends ... with FragmentA.OnArticleSelectedListener
and extends ... with OnArticleSelectedListener
displays the error not found: type FragmentA
or not found: type OnArticleSelectedListener
.
Anyone an idea how to solve this problem?