Android Firestore分页startAfter(DocumentSnapshot)使用java但不使用kotlin
我正在尝试将一个Android Firestore项目从java转换为kotlin。但在分页部分遇到了问题,其中使用java代码的startAfter(DocumentSnapshot)工作正常。但是科特林一号只给出了前3个结果。StartAfter(DocumentSnapshot)部件不工作Android Firestore分页startAfter(DocumentSnapshot)使用java但不使用kotlin,android,kotlin,google-cloud-firestore,pagination,Android,Kotlin,Google Cloud Firestore,Pagination,我正在尝试将一个Android Firestore项目从java转换为kotlin。但在分页部分遇到了问题,其中使用java代码的startAfter(DocumentSnapshot)工作正常。但是科特林一号只给出了前3个结果。StartAfter(DocumentSnapshot)部件不工作 private fun loadNotes() { val query = if (lastResult == null) { notebookRef
private fun loadNotes() {
val query = if (lastResult == null) {
notebookRef
.orderBy("priority")
.limit(3)
} else {
Log.d(TAG, "loadNotes: ${lastResult!!.id}")
notebookRef
.orderBy("priority")
.startAfter(lastResult)
.limit(3)
}
Log.d(TAG, "loadNotes: $query")
query.get()
.addOnSuccessListener { QuerySnapshot ->
var text = ""
for (queryDocumentSnapshot in QuerySnapshot) {
val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
note.docID = queryDocumentSnapshot.id
val title = note.title
val description = note.description
val priority = note.priority
text += "ID: ${note.docID} \n Title : $title\n Description :$description\n" +
"Priority :$priority \n"
}
if (QuerySnapshot.size() > 0) {
text += "---------------\n\n"
textView_data.append(text)
lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
}
}
}
如果你能指出我在科特林出了什么问题,那将非常有帮助
这是运行良好的java代码
public void loadNotes(View v) {
Query query;
if (lastResult == null) {
query = notebookRef.orderBy("priority")
.limit(3);
} else {
query = notebookRef.orderBy("priority")
.startAfter(lastResult)
.limit(3);
}
Log.d(TAG, "loadNotes: "+ query);
query.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String data = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
Notee note = documentSnapshot.toObject(Notee.class);
note.setDocumentId(documentSnapshot.getId());
String documentId = note.getDocumentId();
String title = note.getTitle();
String description = note.getDescription();
int priority = note.getPriority();
data += "ID: " + documentId
+ "\nTitle: " + title + "\nDescription: " + description
+ "\nPriority: " + priority + "\n\n";
}
if (queryDocumentSnapshots.size() > 0) {
data += "___________\n\n";
textViewData.append(data);
lastResult = queryDocumentSnapshots.getDocuments()
.get(queryDocumentSnapshots.size() - 1);
}
}
});
}
希望能得到一些帮助
谢谢
测试所需的代码:并且您的代码按预期工作。您在Kotlin中所做的与在java中所做的没有区别。在这两种情况下,您都将限制指定为3
如果有任何错误,它将出现在您的逻辑上。您的代码按预期工作。您在Kotlin中所做的与在java中所做的没有区别。在这两种情况下,您都将限制指定为3
如果有任何错误,它将出现在您的逻辑上。您必须在Kotlin中以不同的方式实现它。与保留对最后一个结果的引用不同,您必须保留对使用最后一个结果创建的查询的引用 删除全局lastResult并将其替换为:
private var query: Query? = null
然后实现LoadNotes,如下所示:
private fun loadNotes() {
if(query == null){
query = notebookRef.orderBy("priority").limit(3)
}
query!!.get().addOnSuccessListener { QuerySnapshot ->
var text = ""
for (queryDocumentSnapshot in QuerySnapshot) {
val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
note.docID = queryDocumentSnapshot.id
val title = note.title
val description = note.description
val priority = note.priority
text += "ID: ${note.docID}\nTitle: $title \nDescription: $description"+
"\nPriority: $priority\n"
}
if (QuerySnapshot.size() > 0) {
text += "---------------\n\n"
textView_data.append(text)
val lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
query = notebookRef.orderBy("priority").startAfter(lastResult).limit(3)
}
}
}
最后两行是它工作的原因。老实说,我仍然不明白为什么它不像Java那样工作,但至少这个实现是有效的。还建议像这样实现它。您必须在Kotlin中以不同的方式实现它。与保留对最后一个结果的引用不同,您必须保留对使用最后一个结果创建的查询的引用 删除全局lastResult并将其替换为:
private var query: Query? = null
然后实现LoadNotes,如下所示:
private fun loadNotes() {
if(query == null){
query = notebookRef.orderBy("priority").limit(3)
}
query!!.get().addOnSuccessListener { QuerySnapshot ->
var text = ""
for (queryDocumentSnapshot in QuerySnapshot) {
val note: Note = queryDocumentSnapshot.toObject(Note::class.java)
note.docID = queryDocumentSnapshot.id
val title = note.title
val description = note.description
val priority = note.priority
text += "ID: ${note.docID}\nTitle: $title \nDescription: $description"+
"\nPriority: $priority\n"
}
if (QuerySnapshot.size() > 0) {
text += "---------------\n\n"
textView_data.append(text)
val lastResult = QuerySnapshot.documents[QuerySnapshot.size() - 1]
query = notebookRef.orderBy("priority").startAfter(lastResult).limit(3)
}
}
}
最后两行是它工作的原因。老实说,我仍然不明白为什么它不像Java那样工作,但至少这个实现是有效的。在遇到同样的问题后,我们也建议像这样实现它。好吧。通过使用非空强制转换(!!)解决了此问题。例如:
val query = transactionsCollection
.orderBy(Field.CREATED, Query.Direction.DESCENDING)
.startAfter(lastDocument!!)
.limit(PAGE_SIZE)
在您的情况下,它将是:
query = notebookRef.orderBy("priority")
.startAfter(lastResult!!)
.limit(3);
在经历了同样的问题之后,它正在对Kotlin进行研究。嗯。通过使用非空强制转换(!!)解决了此问题。例如:
val query = transactionsCollection
.orderBy(Field.CREATED, Query.Direction.DESCENDING)
.startAfter(lastDocument!!)
.limit(PAGE_SIZE)
在您的情况下,它将是:
query = notebookRef.orderBy("priority")
.startAfter(lastResult!!)
.limit(3);
有了这一点,它就可以在Kotlin上工作。对于真正的Kotlin支持,您可以将
onSucessListeners
与Kotlin协程进行交换。
是真正的Kotlin支持的一个示例,您可以将onSucessListeners
与Kotlin协程进行交换。
是一个示例我的逻辑是正确的,这就是为什么我明确提到java代码工作正常。@Shijilal java代码中的查询部分与kotlin代码中的查询部分相同,并且应该给出相同的结果。如果有问题,它在代码的另一部分。它只是一个按钮点击,它不受其他代码的影响。为了检查和测试,我已经用所有必需的代码进行了完整的问题更新。我的逻辑是正确的,这就是为什么我清楚地提到java代码运行良好的原因。@Shijilal java代码中的查询部分与kotlin代码中的查询部分相同,并且应该给出相同的结果。如果有问题,它在代码的另一部分。它只是一个按钮点击,它不受其他代码的影响。我已通过问题更新了所有要求的完整代码,用于检查和测试。谢谢。成功了。但问题仍然存在,为什么早期的代码不起作用:(谢谢。它成功了。但问题仍然存在,为什么早期的代码不起作用。:(最后是唯一有效的答案!非常感谢!(我不知道为什么它一开始就不起作用)。最后是唯一有效的答案!非常感谢!(我不确定为什么它一开始就不起作用)。