我正在使用一个product room并将firestore数据存储在一个列表中,然后在该列表上执行搜索,但我得到的搜索结果是空数组 ''包装ml.daily.basket; 导入android.os.Bundle; 导入android.util.Log; 导入androidx.annotation.Nullable; 导入androidx.appcompat.app.appcompat活动; 导入androidx.appcompat.widget.SearchView; 导入androidx.recyclerview.widget.recyclerview; 导入com.google.firebase.firestore.CollectionReference; 导入com.google.firebase.firestore.DocumentSnapshot; 导入com.google.firebase.firestore.EventListener; 导入com.google.firebase.firestore.FirebaseFirestore; 导入com.google.firebase.firestore.FirebaseFirestoreException; 导入com.google.firebase.firestore.QuerySnapshot; 导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.Comparator; 导入java.util.List; 导入ml.daily.basket.data.Product; 公共类搜索扩展了AppCompatActivity{ 私人收藏参考收藏参考; 回收视图回收视图; 搜索视图搜索视图; 私有verticalProductScrollAdapter; 私有FirebaseFirestore数据库; private List productData=new ArrayList(); @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u search); recyclerView=findViewById(R.id.search\u结果); searchView=(searchView)findviewbyd(R.id.searchView); db=FirebaseFirestore.getInstance(); getData(); if(searchView!=null){ searchView.setOnQueryTextListener(新的searchView.OnQueryTextListener(){ @凌驾 公共布尔值onQueryTextSubmit(字符串查询){ //Toast.makeText(MainActivity.this,“搜索”+查询,Toast.LENGTH_LONG.show(); 搜索用户(查询); 返回true; } @凌驾 公共布尔onQueryTextChange(字符串newText){ //Toast.makeText(MainActivity.this,“SEARCH”+newText,Toast.LENGTH_LONG.show(); 搜索用户(新文本); 返回false; } }); } }
此方法从firebase获取产品,但在日志中,我得到的是一个空数组我正在使用一个product room并将firestore数据存储在一个列表中,然后在该列表上执行搜索,但我得到的搜索结果是空数组 ''包装ml.daily.basket; 导入android.os.Bundle; 导入android.util.Log; 导入androidx.annotation.Nullable; 导入androidx.appcompat.app.appcompat活动; 导入androidx.appcompat.widget.SearchView; 导入androidx.recyclerview.widget.recyclerview; 导入com.google.firebase.firestore.CollectionReference; 导入com.google.firebase.firestore.DocumentSnapshot; 导入com.google.firebase.firestore.EventListener; 导入com.google.firebase.firestore.FirebaseFirestore; 导入com.google.firebase.firestore.FirebaseFirestoreException; 导入com.google.firebase.firestore.QuerySnapshot; 导入java.util.ArrayList; 导入java.util.Collections; 导入java.util.Comparator; 导入java.util.List; 导入ml.daily.basket.data.Product; 公共类搜索扩展了AppCompatActivity{ 私人收藏参考收藏参考; 回收视图回收视图; 搜索视图搜索视图; 私有verticalProductScrollAdapter; 私有FirebaseFirestore数据库; private List productData=new ArrayList(); @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity\u search); recyclerView=findViewById(R.id.search\u结果); searchView=(searchView)findviewbyd(R.id.searchView); db=FirebaseFirestore.getInstance(); getData(); if(searchView!=null){ searchView.setOnQueryTextListener(新的searchView.OnQueryTextListener(){ @凌驾 公共布尔值onQueryTextSubmit(字符串查询){ //Toast.makeText(MainActivity.this,“搜索”+查询,Toast.LENGTH_LONG.show(); 搜索用户(查询); 返回true; } @凌驾 公共布尔onQueryTextChange(字符串newText){ //Toast.makeText(MainActivity.this,“SEARCH”+newText,Toast.LENGTH_LONG.show(); 搜索用户(新文本); 返回false; } }); } },java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,此方法从firebase获取产品,但在日志中,我得到的是一个空数组 '''package ml.daily.basket; import android.os.Bundle; import android.util.Log; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; impo
'''package ml.daily.basket;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import ml.daily.basket.data.Product;
public class search extends AppCompatActivity {
private CollectionReference collectionReference;
RecyclerView recyclerView;
SearchView searchView;
private verticalProductScrollAdapter adapter;
private FirebaseFirestore db;
private List<Product> productData=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
recyclerView = findViewById(R.id.search_Results);
searchView = (SearchView)findViewById(R.id.searchView);
db = FirebaseFirestore.getInstance();
getData();
if (searchView!=null){
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(MainActivity.this, "SEARCH " + query, Toast.LENGTH_LONG).show();
searchUsers(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
//Toast.makeText(MainActivity.this, "SEARCH " + newText, Toast.LENGTH_LONG).show();
searchUsers(newText);
return false;
}
});
}
}
private void getData(){
db.collection(“milk”).whereEqualTo(“milk”,1).addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@Nullable QuerySnapshot值,@Nullable FirebaseFirestoreException错误){
if(错误!=null){
System.err.println(“侦听失败:+错误”);
返回;
}
Log.d(“ds”,值+“=>”);
用于(文档快照文档:值){
Product Product=doc.toObject(Product.class);
productData.add(产品);
}
更新产品(productData);
}
});
}
私有void searchUsers(字符串recherche){
如果(recherche.length()>0)
recherche=recherche.substring(0,1.toUpperCase()+recherche.substring(1.toLowerCase();
列表结果=新建ArrayList();
对于(产品:productData){
如果(product.getProductName()!=null&&product.getProductName().contains(recherche)){
结果:添加(产品);
}
}
更新产品(结果);
}
在recycer视图中设置更新的产品
private void getData(){
db.collection("milk").whereEqualTo("milk",1).addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null){
System.err.println("Listen failed:" + error);
return;
}
Log.d("ds", value + " => ");
for (DocumentSnapshot doc: value){
Product product = doc.toObject(Product.class);
productData.add(product);
}
updateProduct(productData);
}
});
}
private void searchUsers(String recherche) {
if (recherche.length() > 0)
recherche = recherche.substring(0, 1).toUpperCase() + recherche.substring(1).toLowerCase();
List<Product> results = new ArrayList<>();
for(Product product : productData){
if(product.getProductName() != null && product.getProductName().contains(recherche)){
results.add(product);
}
}
updateProduct(results);
}
private void updateProduct(列出产品数据){
//按id对列表排序
private void updateProduct(List<Product> productData) {
Collections.sort(productData,新的Comparator(){
@凌驾
公共整数比较(产品o1、产品o2){
int res=-1;
if(o1.getId()>(o2.getId()){
res=1;
}
返回res;
}
});
Log.d(“搜索是”,String.valueOf(productData));
}
}'''
行中的Product Product=doc.toObject(Product.class)代码>您是否获得了一些价值?是的,我做了一些更改,现在我可以搜索,但我无法按照已创建的房间在回收器视图中显示它。意味着我无法显示搜索到的产品出现绑定问题。
Collections.sort(productData, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
int res = -1;
if (o1.getId() > (o2.getId())) {
res = 1;
}
return res;
}
});
Log.d("search is", String.valueOf(productData));
}
}'''