dear Mark,
for some reason I couldn’t post on my previous topic. it wanted me to authorize something.
in my app, I feel that I should be successful in retrieving the list of users I follow, but I get ‘null’ instead.
my code is:
StringBuilder whereClause = new StringBuilder();
whereClause.append( "Users[Following]" );
whereClause.append( ".objectId='" ).append( user.getObjectId() ).append("'");
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause(whereClause.toString());
Backendless.Persistence.of(Users.class).find( dataQuery, new AsyncCallback<BackendlessCollection<Users>> () {
@Override
public void handleResponse(BackendlessCollection<Users> foundUsers) {
// all user instances have been found
// get into list
for (int i = 0; i < foundUsers.getTotalObjects(); i++) {
ListData ld = new ListData();
ru = foundUsers.getCurrentPage().get(i); //ru = returned users. it's defined as a BackendlessUser.
ld.setDescription(String.valueOf(ru.getEmail()));
myList.add(ld);
}
lvDetail.setAdapter(new MyBaseAdapter(context, myList));
I get the right amount of followed users in the “foundUsers.getTotalObjects()” but I get “null” in the
" ld.setDescription(String.valueOf(ru.getEmail())); "
so something is wrong inside the ‘for’ loop, but I’m sure that " ru = foundUsers.getCurrentPage().get(i);" should work in giving me a single User class object.
please help - what do you think is wrong and is giving me a null result ?
Hi Gil,
Here’s a simple way to diagnose the problem. Get the whereClause that your code generates (as a string). Open Backendless console, and go to the table which you’re sending the query to (it is Users in this case). In the search bar paste the whereClase and make sure to set the SQL Search toggle to ON. The search results you see is what backendless will return back to you. This would be the first things to check. If it returns what you expected, then I would use a debugger to check that the code is working properly.
Regards,
Mark
Dear Mark,
when I do the search (" Users[Following].objectId = ‘AF81F07A… …30400’ " it shows me the right users who are followed by that user.
but still when I use, in the app, the syntex:
ru = foundUsers.getCurrentPage().get(i); and then try to use " (String.valueOf(ru.getEmail())); " I get that ru.getEmail = null;
could it be something at the backend itself ? or in the way I defined my User.class in the app ?
frustrating… stuck on this for more than a week and it’s the only thing left in my app.
Hi Gil,
What is the return type of the getEmail() method? If it is already String, then why do you call String.valueOf ?
Try to use a debugger to see what value you get back from the server.
Regards,
Mark
I’ve removed the String.valueOf (it was there because I tested all sorts of way to get it working).
could it be something with my Users.class ?
I THINK (don’t remember exactly) that I’ve downloaded it “as is” from Backendless code generator, though I’m not sure, maybe I’ve written it myself and forgot something. for example in my Status.class I’ve defined the variables like “private String email;” and in here I see that I don’t have such variables, but I use " return super.getEmail();". maybe that’s my error ?
package com.example.gil.ex;
import com.backendless.BackendlessUser;
import com.backendless.geo.GeoPoint;
/**
* Created by Gil on 2/23/2016.
*/
public class Users extends BackendlessUser {
public String getEmail()
{
return super.getEmail();
}
public void setEmail( String email )
{
super.setEmail( email );
}
public String getPassword()
{
return super.getPassword();
}
public String getAbout()
{
return (String) super.getProperty( "about" );
}
public void setAbout( String about )
{
super.setProperty( "about", about );
}
public String getAge()
{
return (String) super.getProperty( "age" );
}
public void setAge( String age )
{
super.setProperty( "age", age );
}
public String getCountry()
{
return (String) super.getProperty( "country" );
}
public void setCountry( String country )
{
super.setProperty( "country", country );
}
public String getFirst_name()
{
return (String) super.getProperty( "first_name" );
}
public void setFirst_name( String first_name )
{
super.setProperty( "first_name", first_name );
}
public String getGender()
{
return (String) super.getProperty( "gender" );
}
public void setGender( String gender )
{
super.setProperty( "gender", gender );
}
public String getLast_name()
{
return (String) super.getProperty( "last_name" );
}
public void setLast_name( String last_name )
{
super.setProperty( "last_name", last_name );
}
public java.util.List<Users> getFollowers()
{
return (java.util.List<Users>) super.getProperty( "Followers" );
}
public void setFollowers( java.util.List<Users> Followers )
{
super.setProperty( "Followers", Followers );
}
public java.util.List<Users> getFollowing()
{
return (java.util.List<Users>) super.getProperty( "Following" );
}
public void setFollowing( java.util.List<Users> Following )
{
super.setProperty( "Following", Following );
}
public GeoPoint getLocation()
{
return (GeoPoint) super.getProperty( "location" );
}
public void setLocation( GeoPoint location )
{
super.setProperty( "location", location );
}
}
Get rid of that Users class. Extending BackendlessUser is a bad idea. I believe we removed it from our code generator as well. Use strictly BackendlessUser in your code. (we will change BackendlessUser to make it final).
Thanks. I had a feeling it has something to do with the Users.class.
trying to remove it.
after removing the Users.class, I can’t retrieve the data I need from the user:
Backendless.Persistence.of(Users.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Users>>() {
the “Users” in “Users.class” and “Users” doesn’t work.
Change it to Backendless.Persistence. of( BackendlessUser.class )
dear Mark, works like a charm!
so my lesson is - never create a Users class.
BTW, before you wrote the last comment, I began writing something like this, would it have worked also ? :
following = (List<BackendlessUser>) user.getProperty("Following")>;
for (int i = 0; i < following.lastIndexOf(following); i++) {
ListData ld = new ListData();
ru = following.getCurrentPage().get(i);
ld.setDescription(ru.getEmail());
Log.i("response", "handleResponse: " + ru.getEmail() + following.getTotalObjects());
myList.add(ld);
}
I am glad you got it working!