4
votes

this is my data in Firestore I want to show this name "mouad"

This is my code

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

               }
           });

        }
    }
    }

This is error

error

2
Please add the code in text format and not in a picture.Alex Mamo
Post the whole activity code @mouad ziziRaj
Remove this lines from comment - // mMainList.setLayoutManager(new LinearLayoutManager(this)); // mMainList.setAdapter(adapterRe); and your error will be resolved.Raj
Did that worked for you @mouadzizi ?Raj
no it doesn't worckmouad zizi

2 Answers

2
votes

Typically this error appears when you are trying to set the adapter from a background thread and not from the "main" thread (for example inside the onCreate() method).

If you are trying to set the adapter from a "delayed" method like inside the onEvent() method, this warning will always appear.

To solve this, move the creation of the adapter in the same thread and get the following line of code out of that for loop.

adapterRe.notifyDataSetChanged();
3
votes

Do you have an empty constructor inside the "Movies.java" file?

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