Skip to content Skip to sidebar Skip to footer

Incompatible Types: List Cannot Be Converted To ArrayList

Am trying to pass a List<> from an activity to a fragment. When an imageView is clicked; the onClick event triggers a call to the server that takes the userId of the user th

Solution 1:

Use this

Bundle args= new Bundle();  
args.putParcelableArrayList(POST_TOTAL, postTotal);
fragment.setArguments(bundle);

instead of

 Bundle args = new Bundle();
 args.putString(POST_TOTAL, postTotal);
 fragment.setArguments(args);

After this you can get back this data by using

Bundle extras = getIntent().getExtras();  
ArrayList<ObjectName> arraylist  = extras.getParcelableArrayList("arraylist");

Solution 2:

That's because you have Lists which are not ArrayLists.

A solution might be to wrap your list into an ArrayList with one of its constructors:

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

Solution 3:

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.

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;
}

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

that is my Answer


Post a Comment for "Incompatible Types: List Cannot Be Converted To ArrayList"