gpt4 book ai didi

java - 如何使用接口(interface)正确实现多态性?

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:18 25 4
gpt4 key购买 nike

我有 2 个模型类(数据、标题),其中包含相同的字段:字符串数据ID。我想通过接口(interface)实现来获取这两个 ID。

我通过 Bundle 将标题模型传递到另一个 Activity,在同一 Activity 中通过 Bundle 传递数据模型(仅创建该 Activity 的新实例并重置信息)。

我希望我的两个模型类都使用 String getSharedId() 方法实现 SharedID 接口(interface);

如何从不同的型号获得不同的 id?我只需要放置一个参数,它应该是我的 ViewModelFactory 构造函数中的 String。

public class Data implements SharedId,Parcelable {

private String text;
private String textHeader;
private int viewType;
private String mainId;
private String dataID;

public Data() { }

public String getDataID() {
return dataID;
}

public void setDataID(String dataID) {
this.dataID = dataID;
}

public String getText() {return (String) trimTrailingWhitespace(text); }

public void setText(String text) {
this.text = (String) trimTrailingWhitespace(text);
}

public String getTextHeader() {
return (String) trimTrailingWhitespace(textHeader);
}

public void setTextHeader(String textHeader) {
this.textHeader = textHeader;
}

public int getViewType() {
return viewType;
}

public void setViewType(int viewType) {
this.viewType = viewType;
}

public String getMainId() {
return mainId;
}

public void setMainId(String mainId) {
this.mainId = mainId;
}

protected Data(Parcel in) {
text = in.readString();
textHeader = in.readString();
viewType = in.readInt();
mainId = in.readString();
dataID = in.readString();
}

@Override
public String toString() {
return "Data{" +
"order=" +
", text='" + text + '\'' +
", textHeader='" + textHeader + '\'' +
", viewType=" + viewType +
'}';
}

@SuppressWarnings("StatementWithEmptyBody")
public static CharSequence trimTrailingWhitespace(CharSequence source) {
if (source == null) {
return "";
}
int i = source.length();
// loop back to the first non-whitespace character
while (--i >= 0 && Character.isWhitespace(source.charAt(i))) {
}
return source.subSequence(0, i + 1);
}

public static final Creator<Data> CREATOR = new Creator<Data>() {
@Override
public Data createFromParcel(Parcel in) {
return new Data(in);
}

@Override
public Data[] newArray(int size) {
return new Data[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(text);
dest.writeString(textHeader);
dest.writeInt(viewType);
dest.writeString(mainId);
dest.writeString(dataID);
}

@Override
public String getSharedDataId() {
return getDataID();
}

}

public class Title implements SharedId,Parcelable {

private String dataID;
private String title;

public Title() { }

protected Title(Parcel in) {
dataID = in.readString();
title = in.readString();
}

public String getDataID() {
return dataID;
}

public void setDataID(String dataID) {
this.dataID = dataID;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public static final Creator<Title> CREATOR = new Creator<Title>() {
@Override
public Title createFromParcel(Parcel in) {
return new Title(in);
}

@Override
public Title[] newArray(int size) {
return new Title[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(dataID);
dest.writeString(title);
}

@NonNull
@Override
public String toString() {
return "Title{" +
"dataID='" + dataID + '\'' +
", titleOrder=" +
", title='" + title + '\'' +
'}';
}

@Override
public String getSharedDataId() {
return getDataID();
}
}

我的 DetailActivity 代码,我已经成功地完成了传递 id 的任务,但我需要通过接口(interface)来完成此操作:(所以帮助我的 friend 们,我将非常感激!

public class DetailActivity extends AppCompatActivity implements
DetailAdapter.OnDialogClickListener,
DetailAdapter.OnDetailClickListener {
private static String id;
private String parentId;
private Data data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

TextView tvToolbarTitle = findViewById(R.id.title_toolbar_detail);
tvToolbarTitle.setSelected(true);
findViewById(R.id.btn_back).setOnClickListener(v -> finish());

ArrayList<SharedId> sharedIds = new ArrayList<>();
sharedIds.add(new Title());
sharedIds.add(new Data());
for (SharedId sharedId : sharedIds){
System.out.println(sharedId.getSharedDataId());
}

Bundle bundle = getIntent().getExtras();

if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
}
if (bundle != null) {
Title model = bundle.containsKey("ID") ? bundle.getParcelable("ID") : null;
Data childModel = bundle.containsKey("idDetail") ? bundle.getParcelable("idDetail") : null;
String parentId = bundle.getString("mainScreenId");
if (parentId != null) {
this.parentId = parentId;
}
if (model != null) {
this.id = model.getDataID();
tvToolbarTitle.setText(model.getTitle());
}
if (childModel != null) {
this.id = childModel.getDataID();
tvToolbarTitle.setText(childModel.getTextHeader());
}
}
RecyclerView recyclerView = findViewById(R.id.rv_detail);
DetailAdapter adapter = new DetailAdapter(this, this);
recyclerView.setAdapter(adapter);
// TODO: 3/1/19 change it to single ID // DetailViewModelFactory(); // id != null ? id : parentId
DetailViewModelFactory detailViewModelFactory = new DetailViewModelFactory(id != null ? id : parentId);
DetailActivityViewModel viewModel = ViewModelProviders.of(this, detailViewModelFactory).get(DetailActivityViewModel.class);
FirebaseListLiveData<Data> liveData = viewModel.getLiveDataQuery();
liveData.observe(this, adapter::setNewData);
}

@Override
public void onDialogClicked(@NonNull String text) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(HtmlCompat.fromHtml(text, 0, null, new HandlerHtml()));
builder.setPositiveButton("Ok", null);
builder.show();
}

@Override
public void onDetailClicked(Data data) {
Intent intent = new Intent();
DetailActivity.open(DetailActivity.this);
intent.putExtra("idDetail", data);
intent.putExtra("mainScreenId", id);
startActivity(intent);
}
public static void open(@NonNull Context context) {
context.startActivity(new Intent(context, InfoActivity.class));
}
}

最佳答案

我发现了一个有点不同但有效的解决方案!

我创建一个界面

public interface SharedId {
String getSharedDataId();
String getHeader();
}

我的两个模型类 Data + Title 都实现了接口(interface)和方法。 在 DetailActivity 中我创建了 2 个字符串。 私有(private)字符串mainId; 私有(private)字符串详细信息 ID;然后用我的模型类和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);

关于java - 如何使用接口(interface)正确实现多态性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54988349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com