Search Firestore query don't show data in RecycleView
这是我在Firestore中的数据
我想把这个名字显示为"mouad"
这是我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | ublic class SearchActivity extends AppCompatActivity { private RecyclerView mMainList; private FirebaseFirestore mFirestore; private List usersList; private CustomAdapter adapterRe; EditText editText; Button btnSearch; String name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_firebase); mFirestore = FirebaseFirestore.getInstance(); editText = (EditText) findViewById(R.id.search); btnSearch = (Button) findViewById(R.id.btn); usersList = new ArrayList(); adapterRe = new CustomAdapter(getApplicationContext(), usersList); mMainList = (RecyclerView) findViewById(R.id.recyvle); // mMainList.setHasFixedSize(true); // mMainList.setLayoutManager(new LinearLayoutManager(this)); // mMainList.setAdapter(adapterRe); btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SearchUserFirebase(); } }); } private void SearchUserFirebase() { name = editText.getText().toString(); if(!name.isEmpty()){ Query query = mFirestore.collection("Movies").orderBy("name" ).startAt(name).endAt(name +"\uf8ff"); query.addSnapshotListener(new EventListener() { @Override public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) { if (e != null){ Log.d("TAG","Error :" + e.getMessage()); } ArrayList adsList = new ArrayList(); for(DocumentChange doc : documentSnapshots.getDocumentChanges()){ if (doc.getType() == DocumentChange.Type.ADDED){ Movies users = doc.getDocument().toObject(Movies.class); usersList.add(users); adapterRe.notifyDataSetChanged(); } } Log.d("TAG","no of records of the search is" + adsList.size()); } }); } } } |
这是错误的
错误
当您尝试从后台线程而不是"主"线程(例如在
如果您尝试通过
要解决这个问题,请将适配器的创建移到同一个thead中,然后从for循环中获取以下代码行。
1 | adapterRe.notifyDataSetChanged(); |