2017-07-06 104 views
0

我試圖將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> 
+0

'args.putStringArrayList'不以工作列表不是** StringArrayList ** –

回答

0

使用此

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

這個問題似乎不是關於'String'值* postTotal *,所以你的(基本上是正確的)方法可能會被誤解。還應該提到OP必須讓每個問題類[實現Parcelable](https://developer.android.com/reference/android/os/Parcelable.html) – 0X0nosugar

0

這是因爲您有List s不是ArrayList s。

一個解決辦法可能是包裝你列到ArrayList其構造函數:

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

我終於得到了一個解決方案。我不得不將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(); 
} 

這就是我的回答

相關問題