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);
添加回答
舉報