我正在处理一个应用程序,我需要将ArrayList模型对象传递到下一个片段,但它抛出了一个错误。我尝试了许多文档,但没有任何工作请指导我为什么会发生这种情况。我正在通过firebase检索数据,并希望在我的recyclerview适配器上设置此ArrayList。我该如何做?请指导我
片段A中的代码
ArrayList<RecyclerviewModel> mydatalist = new ArrayList<>();
HomeFragmentDirections.ActionHomeToSearchDataFragment dr = HomeFragmentDirections.actionHomeToSearchDataFragment(mydatalist);
Navigation.findNavController(view).navigate(dr);
来自片段B的代码(接收片段)
ArrayList<RecyclerviewModel> mylist;
if (getArguments()!=null)
{
SearchDataFragmentArgs args = SearchDataFragmentArgs.fromBundle(getArguments());
mylist = args.getMylist();
adapter = new SearchRecycleAdapter(mylist, getContext());
binding.searchDataRecyclerview.setAdapter(adapter);
}
型号类别
public class RecyclerviewModel implements Parcelable {
String name;
String email;
String desc;
String book;
List<String> images;
String uid;
String searching;
public RecyclerviewModel() {
}
public RecyclerviewModel(String name, String email, String desc, String book, List<String> images, String uid,String searching) {
this.name = name;
this.email = email;
this.desc = desc;
this.book = book;
this.images= images;
this.uid = uid;
this.searching = searching;
}
protected RecyclerviewModel(Parcel in) {
name = in.readString();
email = in.readString();
desc = in.readString();
book = in.readString();
images = in.createStringArrayList();
uid = in.readString();
searching = in.readString();
}
public static final Creator<RecyclerviewModel> CREATOR = new Creator<RecyclerviewModel>() {
@Override
public RecyclerviewModel createFromParcel(Parcel in) {
return new RecyclerviewModel(in);
}
@Override
public RecyclerviewModel[] newArray(int size) {
return new RecyclerviewModel[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getBook() {
return book;
}
public void setBook(String book) {
this.book = book;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getSearching() {
return searching;
}
public void setSearching(String searching) {
this.searching = searching;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(email);
parcel.writeString(desc);
parcel.writeString(book);
parcel.writeStringList(images);
parcel.writeString(uid);
parcel.writeString(searching);
}
}
导航图
错误,正在抛出
error: incompatible types: ArrayList<RecyclerviewModel> cannot be converted to RecyclerviewModel
2条答案
按热度按时间k5ifujac1#
您应该创建另一个空类来扩展ArrayList,如下所示:
然后将导航参数的类型设置为此类。
您的
RecyclerViewModel
也应该是Parcelable
ma8fv8wu2#
如果你不知道,你就在某个地方尝试做这样的事情:
因此,请再次检查您代码。
你可以使用一个类和接口来传递数据(这不是最好的方法,但我使用它)
在片段2(接收器)中:
在分段1中(发送者):
我希望这对你有帮助:)