2017-07-06 4 views
0

Ich versuche, eine List<> von einer Aktivität an ein Fragment zu übergeben.Inkompatible Typen: Liste <FollowUser> kann nicht in ArrayList konvertiert werden <String>

Wenn ein ImageView angeklickt wird; Das onClick-Ereignis löst einen Aufruf an den Server aus, der die userId des Benutzers übernimmt, der auf imageView und die userId des Benutzerprofils geklickt hat, das Sie sehen möchten.Ich habe eine Schnittstelle, die die vom Server erhaltene Antwort übernimmt an eine neue Fragmentklasse gesendet. Das Problem ist, dass ich ein List<> durch das Bündel nicht übergeben kann.

Dies ist die newInstance der Fragment Klasse und die Parameter versuchen über

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

zu senden, während diese die Klasse der Aktivität ist, dass das oben genannte Fragment von genannt worden ist.

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

So die folgenden Fehler bin immer:

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' mit einem nicht funktioniert Liste, die keine ** StringArrayList ist ** –

Antwort

0

verwenden

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

statt

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

Danach Sie diese Daten zurück

durch Verwendung erhalten können
+0

Das Problem scheint nicht auf den 'String' Wert * postTotal * zu beruhen, so dass Ihre (im Grunde korrekte) Vorgehensweise missverstanden werden kann. Man sollte auch erwähnen, dass OP jede der Problemklassen [implementieren Parcelable] haben muss (https://developer.android.com/reference/android/os/Parcelable.html). – 0X0nosugar

0

Das ist, weil Sie List s haben, die nicht ArrayList s sind.

Eine Lösung könnte sein, Ihre Liste in einem ArrayList mit einem seiner Erbauer zu wickeln:

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

ich endlich eine Lösung. Ich musste Parcelable zu UploadPost-Modellklasse und FollowUser-Modellklasse implementieren. Da diese beiden Modellklassen in einer dritten Klasse aufgerufen werden, die sie ebenso wie andere Parameter enthält, die vom Server gesendet werden, musste ich Parcelable in die Modellklasse implementieren.

Dann: newInstance Methode der Fragment-Klasse:

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

und Aktivitätsklasse Aufruf 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(); 
} 

, dass meine Antwort ist

Verwandte Themen