Hello. I have a relational data (1:N) column in Users table. (autoload checked),
and i want to get this column in an arraylist/list from the current user object.
I tried
BackendlessUser currentUser = Backendless.UserService.CurrentUser();
Celebs[] followings = (Celebs[])currentUser.getProperty("following");
Collection<Celebs> followings = (Collection<Celebs>) currentUser.getProperty("following");
List<Celebs> followings = (List<Celebs>) currentUser.getProperty("following");
but all of these throw an error “java.util.HashMap[] cannot be cast to java.util.List”. What do I do?
Hi Zeba,
There are 2 things to keep in mind:
You need to make the following call before you retrieve any data from the server:
Backendless.Data.mapTableToClass( "Celebs", Celebs.class );
See the paragraph starting with "There is a special consideration for a user property containing a collection of related data" on this page in the doc:
https://backendless.com/documentation/users/android/users_user_properties.htm
Regards,
Mark
Hi Mark,
About point 1, I’m not retrieving any data from server, I’m just getting the current logged in user. So where do i use this line? I tried putting it before the login call, and using the code from link u mentioned in point 2, but the problem persists while casting.
When user logs in, an object representing the user is retrieved from the server. I’d add the mapping code right after initApp. The problem will persist as long as you try casting it to List. The collection of related objects for BackendlessUser will always be an array.
Thanks ! I did it. Here’s how:
Object[] followingArray = (Object[]) currentUser.getProperty( "following" );
List followings = new ArrayList();
Collections.addAll(followings, followingArray);
if (followings.size()>0) {
for (Object cel: followings) {
Celebs c2 = (Celebs) cel;
//other code
}
}