不相容的类型:列表无法转换为ArrayList
我试图将List<>
从活动传递到片段。不相容的类型:列表<FollowUser>无法转换为ArrayList <String>
点击imageView时, onClick事件触发对服务器的调用,该服务器接受单击imageView的用户的userId和要查看的用户配置文件的userId。我有一个接口,该接口将从服务器获取响应,然后发送到一个新的片段类。我遇到的问题是我无法通过该软件包通过List<>
。
这是片段类的newInstance
和参数我试图跨越
public static HomeUserProfileFragment newInstance(String postTotal, String followersTotal
, String followingTotal, List<UploadPost> uploadPostList, List<FollowUser> followersList, List<FollowUser> followingList) {
HomeUserProfileFragment fragment = new HomeUserProfileFragment();
Bundle args = new Bundle();
args.putString(POST_TOTAL, postTotal);
args.putString(FOLLOWERS_TOTAL, followersTotal);
args.putString(FOLLOWING_TOTAL, followingTotal);
args.putStringArrayList(UPLOAD_POST_LIST,uploadPostList);
args.putStringArrayList(FOLLOWERS_LIST,followersList);
args.putStringArrayList(FOLLOWING_LIST, followingList);
fragment.setArguments(args);
return fragment;
}
派虽然这是Activity类,上述片段是从调用。
@Override
public void onClickUserProfile(String post, String followers, String following, List<UploadPost> uploadPostList, List<FollowUser> followersList, List<FollowUser> followingList) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, HomeUserProfileFragment.newInstance(post, followers, following,uploadPostList,followersList,followingList))
.addToBackStack(null)
.commit();
}
所以我收到以下错误:
Error:(56, 50) error: incompatible types: List<UploadPost> cannot be converted to ArrayList<String>
Error:(57, 48) error: incompatible types: List<FollowUser> cannot be converted to ArrayList<String>
Error:(58, 49) error: incompatible types: List<FollowUser> cannot be converted to ArrayList<String>
使用此
Bundle args= new Bundle();
args.putParcelableArrayList(POST_TOTAL, postTotal);
fragment.setArguments(bundle);
,而不是
Bundle args = new Bundle();
args.putString(POST_TOTAL, postTotal);
fragment.setArguments(args);
这之后,您可以通过使用
取回该数据Bundle extras = getIntent().getExtras();
ArrayList<ObjectName> arraylist = extras.getParcelableArrayList("arraylist");
这个问题似乎不是关于'String'值* postTotal *,所以你的(基本上是正确的)方法可能会被误解。还应该提到OP必须让每个问题类[实现Parcelable](https://developer.android.com/reference/android/os/Parcelable.html) – 0X0nosugar
这是因为您有List
s不是ArrayList
s。
一个解决办法可能是包装你列到ArrayList
其构造函数:
args.putStringArrayList(UPLOAD_POST_LIST, new ArrayList<>(uploadPostList));
我终于得到了一个解决方案。我不得不将Parcelable实现为UploadPost模型类和FollowUser模型类。由于这两个模型类是在第三个类中调用的,它们保存它们以及从服务器发送的其他参数,所以我必须将Parcelable实现到模型类。
然后:片段类的newInstance方法:
public static HomeUserProfileFragment newInstance(String yourId, String other_UserId, String name,String username,String userImage,String postTotal, String followersTotal
, String followingTotal, ArrayList<UploadPost> uploadPostList, ArrayList<FollowUser> followersList, ArrayList<FollowUser> followingList) {
HomeUserProfileFragment fragment = new HomeUserProfileFragment();
Bundle args = new Bundle();
args.putString(YOUR_USER_ID, yourId); // this holds the id of the user that his profile wants to be seen.
args.putString(VIEWER_USER_ID, other_UserId); // this holds the id of the user that want to see your profile. this Id is used to determine if the user is a friend or not.so if not a friend the follow button will be displayed.
args.putString(NAME,name);
args.putString(USERNAME, username);
args.putString(USER_IMAGE, userImage);
args.putString(POST_TOTAL, postTotal);
args.putString(FOLLOWERS_TOTAL, followersTotal);
args.putString(FOLLOWING_TOTAL, followingTotal);
args.putParcelableArrayList(UPLOAD_POST_LIST, uploadPostList);
args.putParcelableArrayList(FOLLOWERS_LIST, followersList);
args.putParcelableArrayList(FOLLOWING_LIST, followingList);
fragment.setArguments(args);
return fragment;
}
而且Activity类调用片段:
@Override
public void onClickUserProfile(String yourId, String viewerId, String name, String username, String userImage, String post,
String followers, String following, ArrayList<UploadPost> uploadPostList, ArrayList<FollowUser> followersList, ArrayList<FollowUser> followingList) {
this.yourId = yourId;
this.viewerId = viewerId;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, HomeUserProfileFragment.newInstance(yourId,viewerId,name, username,userImage,post, followers, following,uploadPostList,followersList,followingList))
.addToBackStack(null)
.commit();
}
这就是我的回答
'args.putStringArrayList'不以工作列表不是** StringArrayList ** –