不兼容的类型:List< FollowUser>无法转换为ArrayList< String> [英] Incompatible types: List<FollowUser> cannot be converted to ArrayList<String>

查看:41
本文介绍了不兼容的类型:List< FollowUser>无法转换为ArrayList< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am试图将 List<> 从活动传递到片段.

Am trying to pass a List<> from an activity to a fragment.

当单击imageView时;onClick事件触发对服务器的调用,该调用采用单击imageView的用户的userId和要查看的用户配置文件的userId.i有一个接口,该接口可以接收从服务器获得的响应,然后发送到新的片段类.问题是我无法通过捆绑包传递 List<> .

When an imageView is clicked; the onClick event triggers a call to the server that takes the userId of the user that clicked on the imageView and the userId of the users profile you want to see.i have an interface that that takes the response gotten from the server, which is then sent to a new fragment class. The problem am having is that i cannot pass a List<> through the bundle.

这是片段类的 newInstance ,并且该参数正尝试通过

This is the newInstance of the fragment class and the parameter am trying to send across

  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类.

While this is the the Activity class that the Above Fragment is been called from.

 @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>

推荐答案

我终于找到了解决方案.我必须实现Parcelable到UploadPost模型类和FollowUser模型类.由于这两个模型类在包含它们的第三个类以及从服务器发送的其他参数中被调用,因此我必须对模型类实现Parcelable.

I finally got a solution. I had to implement Parcelable to UploadPost model class and FollowUser model class. Since these two model class are called in a third class that hold them as well as other parameters that are sent from the server, i had to implement Parcelable to the model class.

然后:Fragment类的newInstance方法:

Then: newInstance method of Fragment class:

 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类调用Fragment:

And Activity class calling Fragment:

    @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();
}

那是我的答案

这篇关于不兼容的类型:List&lt; FollowUser&gt;无法转换为ArrayList&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆