我正在尝试根据文档中某个字段中的关键字显示搜索结果,以显示来自Firestore集合的文档列表。
我看到firestore的操作指南显示如下内容:
citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco");
我尝试过,但结果完全不正确。
该类显示基于使用intentextra从上一个活动中检索到的关键字的查询结果。
我不确定如何修复它,如果熟悉Firestore的任何人都可以帮助我,我将非常感激。谢谢。
public class SearchActivity extends AppCompatActivity {
private TextView showKeyword;
private RecyclerView recyclerView;
private JournalAdapter adapter;
private List<Journal> journalList;
private FirebaseFirestore db;
private FirebaseAuth mAuth;
private String userId;
private CollectionReference userIdRef;
private String keywordString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
showKeyword = (TextView) findViewById(R.id.keywordTextView);
recyclerView = (RecyclerView) findViewById(R.id.recyclerViewSearch);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
journalList = new ArrayList<>();
adapter = new JournalAdapter(this, journalList);
recyclerView.setAdapter(adapter);
//firestore
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
userId = mAuth.getCurrentUser().getUid();
userIdRef = db.collection("main").document(userId).collection("journal");
showData();
}
private void showData() {
Intent intent = getIntent();
keywordString = intent.getStringExtra("keyword");
showKeyword.setText(keywordString);
//problem, funny result
userIdRef.whereGreaterThanOrEqualTo("description", keywordString)
.orderBy("description")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
assert queryDocumentSnapshots != null;
if (!queryDocumentSnapshots.isEmpty()){
//if not empty
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot documentSnapshot : list) {
Journal journal = documentSnapshot.toObject(Journal.class);
assert journal != null;
journal.setId(documentSnapshot.getId());
journalList.add(journal);
}
adapter.notifyDataSetChanged();
} else {
//if empty
Toast.makeText(SearchActivity.this, "No result with the word " + keywordString + " is found", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SearchActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}