Android 解析:从父级检索子级
我正在使用并创建两个模型之间的一对一关系(一个位置有一个队列)。如何仅使用位置检索队列的属性?我刚刚开始使用Parse。根据它们的特性,在存储它们之前,需要将队列ParseObject添加到位置ParseObject(反之亦然) 假设您将两者之间的关系放在location对象中,您应该能够通过如下方式拉动队列: 存储:Android 解析:从父级检索子级,android,Android,我正在使用并创建两个模型之间的一对一关系(一个位置有一个队列)。如何仅使用位置检索队列的属性?我刚刚开始使用Parse。根据它们的特性,在存储它们之前,需要将队列ParseObject添加到位置ParseObject(反之亦然) 假设您将两者之间的关系放在location对象中,您应该能够通过如下方式拉动队列: 存储: // Create location ParseObject location = new ParseObject("Location"); location.put("foo"
// Create location
ParseObject location = new ParseObject("Location");
location.put("foo", "bar");
// Create queue
ParseObject queue = new ParseObject("Queue");
queue.put("name", "Ben");
// Store the queue in the location (location will contain a pointer to queue)
location.put("Queue", queue);
// Save both location and queue
location.saveInBackground();
// Retrieve location using objectId
ParseQuery query = new ParseQuery("Location");
query.getInBackground("QkKt30WhIA", new GetCallback() { // objectId!
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Location found! Query for the queue
object.getParseObject("Queue").fetchIfNeededInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
// Queue found! Get the name
String queueAttr = object.getString("name");
Log.i("TEST", "name: " + queueAttr);
}
});
}
else {
// something went wrong
Log.e("TEST", "Oops!");
}
}
});
检索:
// Create location
ParseObject location = new ParseObject("Location");
location.put("foo", "bar");
// Create queue
ParseObject queue = new ParseObject("Queue");
queue.put("name", "Ben");
// Store the queue in the location (location will contain a pointer to queue)
location.put("Queue", queue);
// Save both location and queue
location.saveInBackground();
// Retrieve location using objectId
ParseQuery query = new ParseQuery("Location");
query.getInBackground("QkKt30WhIA", new GetCallback() { // objectId!
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Location found! Query for the queue
object.getParseObject("Queue").fetchIfNeededInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
// Queue found! Get the name
String queueAttr = object.getString("name");
Log.i("TEST", "name: " + queueAttr);
}
});
}
else {
// something went wrong
Log.e("TEST", "Oops!");
}
}
});
这就是我最后做的:
// create qLocation
ParseObject qLocation = new ParseObject("QLocations");
qLocation.put("name", qname);
qLocation.put("streetAddress", streetAddress);
qLocation.put("cityState", cityState);
// create qLine
ParseObject qLine = new ParseObject("Lines");
Random r = new Random();
qLine.put("length", r.nextInt(10)); //set line length to a random number between 0-10
qLine.put("qName",qname);
// add relationship
qLine.put("parent", qLocation);
// save line and location
qLine.saveInBackground();
最后,行qline.put(“parent”,qLocation);我不知道如何使用这种关系从给定的位置获取队列,所以结果并不重要。因此,我只是使用qLine中的“qName”列来检查哪个队列与哪个位置关联。感谢您的回复。我试图这样做,但我不明白它如何知道我引用的是哪个队列(因为队列从未被实例化;我获取的位置没有名为“queue”的解析对象)我在队列模型中创建了一列,列的名称是位置,这样我就可以手动创建自己的关系。这样,如果我知道位置,我就可以在队列模型中搜索相同的字符串。这是可行的,但这不是一个非常优雅的方法。我还试图从解析表中获取指针ID,但没有成功或者。当您存储location ParseObject时,是否也创建了一个队列ParseObject,并在调用location.saveInBackground()之前将其放入location ParseObject?我正在尝试编写代码,如果可以的话,将粘贴一些内容。