搜索Firestore查询不会在RecycleView中显示数据

时间:2018-07-20 16:07:16

标签: java android firebase android-recyclerview google-cloud-firestore

这是我在Firestore中的数据 I want to show this name "mouad"

这是我的代码

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());

               }
           });

        }
    }
    }

这是错误

error

2 个答案:

答案 0 :(得分:1)

“ Movies.java”文件中是否有空的构造函数?

 public Movies() {
      //Necessary to retrieve data
 }

答案 1 :(得分:0)

通常,当您尝试从后台线程设置/通知适配器,而从“主”线程(例如,onCreate()方法中)设置/通知适配器时,会出现此错误。 / p>

如果您尝试通过onEvent()方法内部的“延迟”方法来通知适配器,则该错误将始终出现。

要解决此问题,请将适配器的创建移到同一主题中,并从for循环中获取以下代码行。

adapterRe.notifyDataSetChanged();