【问题标题】:Incompatible types: List<FollowUser> cannot be converted to ArrayList<String>不兼容的类型:List<FollowUser> 无法转换为 ArrayList<String>
【发布时间】:2017-07-06 18:07:30
【问题描述】:

我正在尝试将 List&lt;&gt; 从活动传递到片段。

当一个 imageView 被点击时; onClick 事件触发对服务器的调用,该调用获取单击 imageView 的用户的 userId 和您想要查看的用户配置文件的 userId。我有一个接口,可以接收从服务器获得的响应,然后发送到一个新的片段类。我遇到的问题是我无法通过捆绑传递List&lt;&gt;

这是片段类的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>

【问题讨论】:

  • args.putStringArrayList 不适用于非 StringArrayList 的列表

标签: java android android-fragments


【解决方案1】:

使用这个

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");

【讨论】:

  • 问题似乎与StringpostTotal 无关,因此您的(基本上正确的)方法可能会被误解。还应该提到,OP 必须有每个问题类implement Parcelable
【解决方案2】:

那是因为你有Lists 而不是ArrayLists。

一种解决方案可能是将您的列表包装到 ArrayList 及其构造函数之一中:

args.putStringArrayList(UPLOAD_POST_LIST, new ArrayList<>(uploadPostList));

【讨论】:

    【解决方案3】:

    我终于找到了解决办法。我必须实现 Parcelable 到 UploadPost 模型类和 FollowUser 模型类。由于这两个模型类在保存它们以及从服务器发送的其他参数的第三个类中调用,因此我必须将 Parcelable 实现到模型类。

    那么:Fragment类的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类调用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();
    }
    

    这是我的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-17
      • 2016-11-09
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多