Android Parse Object null fileds

时间:2015-12-22 21:08:58

标签: android parse-platform

我正在使用解析器在POST中创建用户类,并在我的仪表板上查看它。 当我尝试查询所有这些对象时,它会返回确切的对象数,但所有字段都为null或0。

 ParseQuery<User> query = ParseQuery.getQuery(PARSE_USER);
            query.findInBackground(new FindCallback<User>() {
                @Override
                public void done(List<User> list, com.parse.ParseException e) {
                    if (e == null) {
                        for (User user : list) {
                            Log.v("user:", user.toString());
                        }
                        //mListView.setAdapter(new LoveBoardAdapter(mCtx, list));
                    } else {
                        Toast.makeText(mCtx, getString(R.string.internet_error), Toast.LENGTH_LONG).show();
                    }
                }
            });

编辑:

这是常量,我的User.class

private final String PARSE_USER = "User";


@ParseClassName("User")
public class User extends ParseObject {

    private int age;
    private String name;
    private String gender;
    private String contact;
    private String bio;
    private String city;
    private int likes;

    public User() {
        super();
    }

    public User(int age, String name, String gender, String contact, String bio, String city, int likes) {
        this.age = age;
        this.name = name;
        this.gender = gender;
        this.contact = contact;
        this.bio = bio;
        this.city = city;
        this.likes = likes;
    }

    public int getLikes() {
        return likes;
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", contact='" + contact + '\'' +
                ", bio='" + bio + '\'' +
                ", city='" + city + '\'' +
                ", likes=" + likes +
                '}';
    }
}

3 个答案:

答案 0 :(得分:2)

我的笔记:

  • Parse注释负责设置数据字段,因此不需要这样做。
  • Parse子类的Getters和Setter应该使用ParseObject类中的访问器。
  • 为类列创建常量,降低拼写错误的风险,并且可以使用代码完成。
  • 在查询中,您应该使用该类以获得更好的类型安全性(UserData.class)
  • 考虑将自定义User类重命名为其他内容(本示例中为UserData),因为它与内置User类发生冲突。 (即使它实际上是_User)
  • 子类可用于在系统User(_User)类中存储数据,但这是一个不同的问题。
  • 记得注册ParseObject.registerSubclass(UserData.class);在MainActivity中

使用以下代码进行测试, UserData类:

@ParseClassName("UserData")
public class UserData extends ParseObject {

    // Setup the constant for the column names here
    public static final String name = "name";
    public static final String city = "city";
    // :

    public UserData() {
    }

    public String getName() {
        // Get data using ParseObject
        return getString(UserData.name);
    }

    public void setName(String newName) {
        // Store data using ParseObject
        put(UserData.name, newName);
    }

    // More setters and getters
    // :
}

ShowUsers活动:

public class ShowUsers extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_users);

        ParseQuery<UserData> query = ParseQuery.getQuery(UserData.class);
        query.findInBackground(new FindCallback<UserData>() {
            @Override
            public void done(List<UserData> objects, ParseException e) {
                if (e == null) {
                    for (UserData userData : objects) {
                        // Access with getter
                        Log.v("user name:", userData.getName());
                        // Access with type safe constant
                        Log.v("city: ", userData.getString(UserData.city));
                    }
                }
            }
        });
    }
}

答案 1 :(得分:1)

由于它是默认用户表,因此请更改:

 ParseQuery<User> query = ParseQuery.getQuery(PARSE_USER);
        query.findInBackground(new FindCallback<User>() { 
            @Override 
            public void done(List<User> list, com.parse.ParseException e) {
                if (e == null) {
                    for (User user : list) {
                        Log.v("user:", user.toString());
                    } 
                    //mListView.setAdapter(new LoveBoardAdapter(mCtx, list)); 
                } else { 
                    Toast.makeText(mCtx, getString(R.string.internet_error), Toast.LENGTH_LONG).show();
                } 
            } 
        }); 

    ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.findInBackground(new FindCallback<User>() { 
            @Override 
             public void done(List<ParseUser> list, ParseException e) {
                if (e == null) {
                    for (User user : list) {
                        Log.v("user:", user.toString());
                    } 
                    //mListView.setAdapter(new LoveBoardAdapter(mCtx, list)); 
                } else { 
                    Toast.makeText(mCtx, getString(R.string.internet_error), Toast.LENGTH_LONG).show();
                } 
            } 
        }); 

答案 2 :(得分:0)

尝试从

更改
@ParseClassName("User")

@ParseClassName("_User")