我已经参考了 2 次以上,并尽我所能理解 2 个片段之间的通信。借助此处上一个问题的帮助 + 2 个参考资料,我能够提出此代码。我必须在我的 FragB 中放入什么来检索用户在 ListFragment FragA 中所做的选择?
主要活动:
public class MainActivity extends Activity implements OnDataPass{
...
@Override
public void onDataPass(String data) {
// TODO Auto-generated method stub
FragA transaction1 = ((FragA) getFragmentManager().findFragmentByTag("ItemRoleList"));
transaction1.dataPasser.onDataPass(data);
}
}
这是FragA:
public class FragA extends ListFragment{
OnDataPass dataPasser;
public interface OnDataPass{
public void onDataPass(String data);
}
@Override
public void onAttach(Activity a) {
super.onAttach(a);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
dataPasser = (OnDataPass) a;
} catch (ClassCastException e) {
throw new ClassCastException(a.toString()
+ " must implement OnHeadlineSelectedListener");
}
}