简体   繁体   中英

How can I create a complete local entity from a ParseObject?

Up until now I've just been using the Parse SDK to query objects directly and then retrieve their properties, ie postObject.getString("bodyText") , postObject.getList("likedBy") , etc.

Let's take a class with the following Parse columns (via the Dashboard):

  • text (String)
  • image (ParseFile)
  • rating (Number)

  • lastReplyUpdatedAt (Date)
  • author (Pointer<_User>)
  • poll (Pointer< Poll >) --- assuming that pointers are different for User and generic classes?
  • likedBy (Array)
  • isPinned (Boolean)

I know how to retrieve Strings, Ints, and ParseFiles, but what about Pointers, Dates, Arrays and Booleans? What do variable declarations and mutators/accessors look like in those cases?

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

    public Post() {

    }

    private String text;
    private ParseFile image;
    private int rating;

    public String getText() {
        return getString("text");
    }

    public void setText(String text) {
        this.text = text;
    }

    public ParseFile getImage() {
        return getParseFile("image");
    }

    public void setImage(ParseFile image) {
        this.image = image;
    }

    public Int getRating() {
        return getInt("rating");
    }

    public void setRating(Int rating) {
        this.rating = rating;
    }
}

Comments are not made for answer, so I'll post it here with a proper example for you to understand. Here's a simple class.

public class A {
    private int number;

    public A(int value){
        this.number = value;
    }
    public void setNumber(int value){
        this.number = value;
    }
    public int getNumber(){
        return this.number();
    }
}       

And here sa more complete class.

public class B{
    private A pointerToA;
    private boolean isAnExample;
    private List<String> myList;
    private Date myDate;

It is completely right to have accessors for each of this field. And their synxtaxes is no different from "classic" accessors such as getNumber() in class B.

    public A getPointerToA(){
        return this.pointerToA;
    }
    public List<String> getMyList(){
        return this.myList;
    }
    public boolean isAnExample(){
        return this.isAnExample;
    }
    public Date getMyDate(){
         return this.myDate;
    }

You can also have "classic" setters for each of these fields, depending on what you're tryign to do.

   public setIsAnExample(boolean bool){
       this.isAnExample = bool;
   }
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM