使用Firestore按键入的字母进行搜索

时间:2018-06-23 23:18:19

标签: android firebase search android-edittext google-cloud-firestore

我尝试使用我在Firestore中键入的字母将搜索集成到EditText集合中。我的意思是,我该如何在没有任何按钮的情况下做到这一点,而只是实时搜索,也就是说,当我在EditText中键入任何字母并且结果出现在ListView中时。

我的班级,在这里我从Firestore中检索了所有记录:

public class AllPlaces extends AppCompatActivity {

private EditText eName;
private ListView mList;

private FirebaseFirestore firestore = FirebaseFirestore.getInstance();
private CollectionReference reference = firestore.collection("Users");
private SearchAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_places);

    mList = findViewById(R.id.listSearch);
    eName = findViewById(R.id.editSearch);

    reference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            List<SearchModel> names = new ArrayList<>();
            if(task.isSuccessful()){
                for(QueryDocumentSnapshot document : task.getResult()) {
                    SearchModel model = document.toObject(SearchModel.class);
                    names.add(model);
                }
                mList = findViewById(R.id.listSearch);
                adapter = new SearchAdapter(AllPlaces.this, names);
                mList.setAdapter(adapter);
            }
        }
    });
}
}

更新:

我找到了这个解决方案,但是当我键入一些字母时,它无法正常工作-ListView中的所有信息都消失了:

eName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                        AllPlaces.this.adapter.getFilter().filter(s);
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });

1 个答案:

答案 0 :(得分:1)

private FirebaseFirestore firestore = FirebaseFirestore.getInstance();
private CollectionReference reference = firestore.collection("Users");
Query q;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_places);

    q = firestore.collection("Users");
    showAdapter(q);

eName.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) {
        if (charSequence.toString().length() == 0) {
            q = firestore.collection("Users");
            showAdapter(q);
         } // This is used as if user erases the characters in the search field.
         else {
             q = reference.orderBy("name").startAt(charSequence.toString().trim()).endAt(charSequence.toString().trim() + "\uf8ff"); // name - the field for which you want to make search
             showAdapter(q);
          }
          adapter.notifyDataSetChanged();
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }
});

void showAdapter(Query q1) {
    q1.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            List<SearchModel> names = new ArrayList<>();
            if(task.isSuccessful()){
                for(QueryDocumentSnapshot document : task.getResult()) {
                    SearchModel model = document.toObject(SearchModel.class);
                    names.add(model);
                }
                mList = findViewById(R.id.listSearch);
                adapter = new SearchAdapter(AllPlaces.this, names);
                mList.setAdapter(adapter);
            }
        }
    });
}

我希望这会有所帮助..