我试图修复两个ParseObjects之间的关系:Place&访问。 我尝试了将对象扩展到ParseObjects的方法,这是一种干净的方法。问题是相关的Place对象没有被保存。
地点:
@ParseClassName("Place")
public class Place extends ParseObject {
public Place() {
}
private String title;
private ParseGeoPoint geoPoint;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public ParseGeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(ParseGeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
//Get distance to current location
public double getDistance(ParseGeoPoint currentLocation) {
return this.geoPoint.distanceInKilometersTo(currentLocation);
}
}
访问
@ParseClassName("Visit")
public class Visit extends ParseObject {
public Visit() {
}
private long timestamp;
private long duration;
private Place place;
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
}
这是我保存到后端的方式:
final Place place = new Place();
place.setTitle("Home");
final Visit visit = new Visit();
visit.setTimestamp(Utils.getUnixNow());
visit.setPlace(place);
visit.saveEventually(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Logger.d("visit " + place.getTitle() + " was saved");
} else {
Logger.d("Place was not saved, error " + e.getCode());
}
}
});
这两个类都在MyApplication类中注册。
答案 0 :(得分:0)
首先:你的Parse类的实现是错误的。
1-删除所有字段变量
2-创建getter和setter,如下所示
public String getTitle() {
return get("Title"); // 'Title' is the column name in your Parse server table
}
public void setTitle(String title) {
put("Title", title);
}
public ParseObject getPlace () { // it's ParseObject not Place
return get("Place "); // 'Place ' is the column name in your Parse server table
}
public void setPlace (ParseObject title) { // it's ParseObject not Place
put("Place ", title);
}
Place place= ParseObject.create(Place .class);
place.setTitle("some title");
Visit visit= ParseObject.create(Visit.class);
visit.setPlace(place);
visit.save(); // or use saveInBackground();
ParseObject.registerSubclass(Visit.class);
ParseObject.registerSubclass(Place.class);
Parse.initialize(this);