Kme*_*mel 0 android google-cloud-firestore
为了让我的用户执行更复杂的搜索,我正在与Algolia合作。
com.algolia.search.saas.Query algolia_query =
new com.algolia.search.saas.Query(mLastQuery)
.setAttributesToRetrieve("content")
.setAttributesToRetrieve("category")
.setHitsPerPage(10);
index.searchAsync(algolia_query, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject jsonObject, AlgoliaException e) {
Log.d("algolia", jsonObject.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
我想将此jsonObject转换为兼容的东西,FirestoreRecyclerOptions以便能够使用FirestoreRecyclerAdapter
谢谢 !
为了更好地理解,我将尝试通过一个示例对此进行解释。假设我们有一个FirebaseFirestore指向数据库根引用的CollectionReference对象和一个指向名为的集合的对象products。为此,我们可以使用以下两行代码:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Run Code Online (Sandbox Code Playgroud)
我们还假设我们有一个Cloud Firestore数据库,看起来像这样:
Firestore-root
|
--- products
|
--- productIdOne
| |
| --- productName: "Milk"
|
--- productIdTwo
|
--- productName: "Soy Milk"
|
--- productIdThree
|
--- productName: "Bacon"
Run Code Online (Sandbox Code Playgroud)
为了实现这种结构,让我们使用以下代码将这些产品以及相应的索引添加到数据库中:
Map<String, Object> mapOne = new HashMap<>();
mapOne.put("productName", "Milk");
Map<String, Object> mapTwo = new HashMap<>();
mapTwo.put("productName", "Soy Milk");
Map<String, Object> mapThree = new HashMap<>();
mapThree.put("productName", "Bacon");
WriteBatch writeBatch = rootRef.batch();
writeBatch.set(productsRef.document(), mapOne);
writeBatch.set(productsRef.document(), mapTwo);
writeBatch.set(productsRef.document(), mapThree);
writeBatch.commit();
Client client = new Client(YourApplicationID, YourAPIKey);
Index index = client.getIndex("products");
List<JSONObject> productList = new ArrayList<>();
productList.add(new JSONObject(mapOne));
productList.add(new JSONObject(mapTwo));
productList.add(new JSONObject(mapThree));
index.addObjectsAsync(new JSONArray(productList), null);
Run Code Online (Sandbox Code Playgroud)
我们还假设我们在out布局文件中有2个视图,分别是an EditText和a ListView。
EditText editText = findViewById(R.id.edit_text);
ListView listView = findViewById(R.id.list_view);
Run Code Online (Sandbox Code Playgroud)
要实际在我们的产品中显示这些产品,请ListView使用以下代码:
productsRef.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
list.add(document.getString("productName"));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Run Code Online (Sandbox Code Playgroud)
为了根据我们输入的文本过滤产品EditText,我们需要添加TextChangedListener如下内容:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable editable) {
Query query = new Query(editable.toString())
.setAttributesToRetrieve("productName")
.setHitsPerPage(50);
index.searchAsync(query, new CompletionHandler() {
@Override
public void requestCompleted(JSONObject content, AlgoliaException error) {
try {
JSONArray hits = content.getJSONArray("hits");
List<String> list = new ArrayList<>();
for (int i = 0; i < hits.length(); i++) {
JSONObject jsonObject = hits.getJSONObject(i);
String productName = jsonObject.getString("productName");
list.add(productName);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
因此,为了使其工作,我们创建了一个新的适配器对象。这意味着,对于我们输入的每个字符EditText,我们都会创建一个新的适配器,并使用来自数据库的结果填充该适配器。为了回答您的问题,为了实现您想要的目标,您不必更改JSONObject,您已经创建了一个新的查询对象,该对象应该传递给setQuery()在该FirestoreRecyclerOptions对象上调用的方法。我只是给了您一个简单的示例来了解流程。有关更多信息,我也建议您观看此视频。
| 归档时间: |
|
| 查看次数: |
2302 次 |
| 最近记录: |