1 回答

TA贡献1893条经验 获得超10个赞
我发现了一些不同但有效的解决方案!
我创建一个界面
public interface SharedId {
String getSharedDataId();
String getHeader();
}
我的两个模型类 Data + Title 都实现了接口和方法。在 DetailActivity 我创建了 2 个字符串。私有字符串 mainId;私有字符串 detailId;然后用我的模型类和 bundle 传递 id
`SharedId mainId = new Title();
SharedId detailId = new Data();
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mainId = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
detailId = bundle.containsKey("idDetail") ?
bundle.getParcelable("idDetail") : null;
}
if (mainId != null) {
this.detailId = mainId.getSharedDataId();
tvToolbarTitle.setText(mainId.getHeader());
}
if (detailId != null) {
this.mainId = detailId.getSharedDataId();
tvToolbarTitle.setText(detailId.getHeader());
}
并传入我的 ViewmodelFactory
DetailViewModelFactory detailViewModelFactory =
new DetailViewModelFactory(this.detailId != null ?
this.detailId : this.mainId);
添加回答
举报