【发布时间】:2019-03-22 21:47:35
【问题描述】:
我正在检索我的 JSON,如下所示: myResponse.java :
public List<Repo> getData() {
return data;
}
public void setData(List<Repo> data) {
this.data = data;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MyResponse)) return false;
MyResponse that = (MyResponse) o;
return data != null ? data.equals(that.data) : that.data == null;
}
@Override
public int hashCode() {
int result = statusCode.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
public static class Repo {
@Expose
@SerializedName("title")
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Repo)) return false;
Repo repo = (Repo) o;
if (!id.equals(repo.id)) return false;
if (!title.equals(repo.title)) return false;
return description.equals(repo.description);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + title.hashCode();
return result;
}
}
现在在我的片段 1 适配器中:
itemView.setOnClickListener(v -> {
AppCompatActivity activity = (AppCompatActivity) itemView.getContext();
activity.getSupportFragmentManager()
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.add(R.id.root_view, Fragment2.newInstance(), Fragment2.TAG)
.commit();
这是我的 fragment2 新实例:
public static Fragment2 newInstance() {
Bundle args = new Bundle();
Fragment2 fragment = new Fragment2();
fragment.setArguments(args);
return fragment;
}
有没有一种方法可以将我从 JSON 检索到的属性标题和描述从片段 1 传递到片段 2? 我想在 fragment2 textview 中显示这些属性,但不确定什么是最好的方法。尝试在网上查找,但到目前为止没有运气。有什么想法吗?
【问题讨论】:
标签: android android-fragments android-fragmentactivity android-databinding android-json