Java 如何将自定义对象传递到不同片段中的列表?
因此,我有我的Java 如何将自定义对象传递到不同片段中的列表?,java,android,Java,Android,因此,我有我的main活动,它有一个BottomNavigationView,在那里我有3个不同的选项卡,当我单击它们时,它们会将我重定向到3个不同的片段 在FragmentA中,我有一个带有项目的RecyclerView,每个项目都有一个按钮。 单击所述按钮时,我想将该对象发送到FragmentB,以便将其添加到ArrayList中,并更新FragmentB中的RecyclerView以显示该项目 唯一的问题是我不知道如何在我点击按钮时发送那个对象 adapter.setOnItemRemov
main活动
,它有一个BottomNavigationView
,在那里我有3个不同的选项卡,当我单击它们时,它们会将我重定向到3个不同的片段
在FragmentA
中,我有一个带有项目的RecyclerView
,每个项目都有一个按钮。
单击所述按钮时,我想将该对象发送到FragmentB
,以便将其添加到ArrayList
中,并更新FragmentB
中的RecyclerView
以显示该项目
唯一的问题是我不知道如何在我点击按钮时发送那个对象
adapter.setOnItemRemoveListener(new RemoveItemAdapter.OnItemRemoveListener() {
@Override
public void onItemRemove(int position) {
//Do I send it from here?
}
});
首先,在您的模型(对象)类中实现Parcelable,然后从片段中调用它-
Fragment fragmentA = new FragmentGet();
Bundle bundle = new Bundle();
bundle.putParcelable("CustomObject", customObject);
fragmentA .setArguments(bundle);
此外,在片段B中,还需要获取参数-
Bundle bundle = getActivity().getArguments();
if (bundle != null) {
model = bundle.getParcelable("CustomObject");
}
您的自定义对象类将如下所示-
public class CustomObject implements Parcelable {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.description);
}
public CustomObject() {
}
protected CustomObject(Parcel in) {
this.name = in.readString();
this.description = in.readString();
}
public static final Parcelable.Creator<CustomObject> CREATOR = new Parcelable.Creator<CustomObject>() {
@Override
public CustomObject createFromParcel(Parcel source) {
return new CustomObject(source);
}
@Override
public CustomObject[] newArray(int size) {
return new CustomObject[size];
}
};
}
public类CustomObject实现可包裹{
私有字符串名称;
私有字符串描述;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地记录(此名称);
目的写入(本说明);
}
公共自定义对象(){
}
受保护的自定义对象(中的地块){
this.name=in.readString();
this.description=in.readString();
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共CustomObject createFromParcel(地块源){
返回新的CustomObject(源);
}
@凌驾
公共CustomObject[]新数组(整数大小){
返回新的CustomObject[大小];
}
};
}
只需从recycler视图项click listener调用片段B,并使用上述代码使用Parcelable传递自定义对象
希望能有帮助 首先在模型(对象)类中实现Parcelable,然后从片段A中调用它-
Fragment fragmentA = new FragmentGet();
Bundle bundle = new Bundle();
bundle.putParcelable("CustomObject", customObject);
fragmentA .setArguments(bundle);
此外,在片段B中,还需要获取参数-
Bundle bundle = getActivity().getArguments();
if (bundle != null) {
model = bundle.getParcelable("CustomObject");
}
您的自定义对象类将如下所示-
public class CustomObject implements Parcelable {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeString(this.description);
}
public CustomObject() {
}
protected CustomObject(Parcel in) {
this.name = in.readString();
this.description = in.readString();
}
public static final Parcelable.Creator<CustomObject> CREATOR = new Parcelable.Creator<CustomObject>() {
@Override
public CustomObject createFromParcel(Parcel source) {
return new CustomObject(source);
}
@Override
public CustomObject[] newArray(int size) {
return new CustomObject[size];
}
};
}
public类CustomObject实现可包裹{
私有字符串名称;
私有字符串描述;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDescription(){
返回说明;
}
公共void集合描述(字符串描述){
this.description=描述;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地记录(此名称);
目的写入(本说明);
}
公共自定义对象(){
}
受保护的自定义对象(中的地块){
this.name=in.readString();
this.description=in.readString();
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共CustomObject createFromParcel(地块源){
返回新的CustomObject(源);
}
@凌驾
公共CustomObject[]新数组(整数大小){
返回新的CustomObject[大小];
}
};
}
只需从recycler视图项click listener调用片段B,并使用上述代码使用Parcelable传递自定义对象
希望能有帮助 那么我将在我的主要活动中实现Parcelable?这不需要两种方法吗?同样在
model=bundle.getParcelable(“CustomObject”)中代码>模型
代表什么?您必须有一个模型类(POJO),其中有一个用于自定义对象的getter setter。Model是您要为其创建ArrayList的POJO类。它基本上是你的自定义对象。我仍然不明白为什么我需要实现Parcelable,我会让interface带来的方法为空吗?共享你的自定义模型类,Parcelable用于将对象数据从和活动或片段传递到另一个活动或片段,Android推荐它,因为它比Serializable快,用于对象的缓冲和取消缓冲。所以我会在我的MainActivity中实现Parcelable?这不需要两种方法吗?同样在model=bundle.getParcelable(“CustomObject”)中代码>模型
代表什么?您必须有一个模型类(POJO),其中有一个用于自定义对象的getter setter。Model是您要为其创建ArrayList的POJO类。它基本上是你的自定义对象。我仍然不明白为什么我需要实现Parcelable,我会让interface带来的方法为空吗?共享你的自定义模型类,Parcelable用于将对象数据从活动或片段传递到另一个活动或片段,Android本身推荐它,因为它比Serializable快,用于对象的缓冲和取消缓冲。可能重复的我实际上使用的是同一个活动,两个不同的片段是相同的,你有一个片段的构造器,这样你就可以使用可能的重复,我实际上在使用同一个活动,两个不同的片段是相同的,你也有一个片段构造器,这样你就可以使用它了