我正在尝试在 android 中为片段添加额外内容。我有一个列表片段,当在列表上单击一个项目时,我想将其 id 作为额外的传递给下一个活动。这是代码:
public class EmailListFragment extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Email mail = ((CustomEmailFragmentAdapter)getListAdapter()).getItem(position);
Intent i = new Intent(getActivity(),EmailFragmentActivity.class);
i.putExtra(EmailFragment.EXTRA_EMAIL_ID, mail.getId());
startActivity(i);
}
}
和抽象的SingleFragmentActivity 类:
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_fragment_container);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.email_fragment_container);
if(fragment == null){
fragment = createFragment();
fm.beginTransaction().add(R.id.email_fragment_container, fragment).commit();
}
}
}
和 EmailFragmentActivity 类:
public class EmailFragmentActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
UUID emailId = (UUID) getIntent().getSerializableExtra(
EmailFragment.EXTRA_EMAIL_ID);
return EmailFragment.newInstance(emailId);
}
}
我将 mail.getId() 作为额外的传递,并在 EmailFragmentActivity 类中获得额外的。问题是,我传递的 id 与我在 EmailFragmentActivity 类中获得的 id 不同。我在这里做错了什么?任何人都可以帮忙吗?
谢谢