在Firebase android

时间:2018-02-11 12:59:37

标签: android firebase search firebase-realtime-database

我正在创建一个可以使用编辑文本和ImageButton搜索数据的应用程序,结果将发布在我的RecyclerView上。我已经可以搜索“fullName”This is my database

但我想要的是搜索“dDate”和“bDate”。

这是我的代码:

public class SearchActivity extends AppCompatActivity {

    private RecyclerView mPerson;
    private ImageButton mButton;
    private EditText mFindField;
    private DatabaseReference mDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        //Database Access//
        mDatabase = FirebaseDatabase.getInstance().getReference().child("person");

        //Recycler View//
        mPerson = (RecyclerView)findViewById(R.id.person_list);
        mPerson.setHasFixedSize(true);
        mPerson.setLayoutManager(new LinearLayoutManager(this));

        mButton = (ImageButton) findViewById(R.id.search_button);
        mFindField = (EditText) findViewById(R.id.search_field);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchText = mFindField.getText().toString();
                onStart(searchText);
            }
        });
    }

    //Recycler Adapter//
    protected void onStart(String searchText){
        super.onStart();
        Query firebaseSearch = mDatabase.orderByChild("fullName").startAt(searchText).endAt(searchText + "\\uf8ff");
        FirebaseRecyclerAdapter<PersonInfo, PersonViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PersonInfo, PersonViewHolder>(
                PersonInfo.class,
                R.layout.recyclerview_adapter,
                PersonViewHolder.class,
                firebaseSearch

        //View Person Info//
        ) {
            @Override
            protected void populateViewHolder(PersonViewHolder viewHolder, PersonInfo model, int position) {
                final String key = getRef(position).getKey();

                viewHolder.setName(model.getFullName());
                viewHolder.setdDate(model.getdDate());

                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent showInfo = new Intent(SearchActivity.this, person_information.class);
                        showInfo.putExtra("Details", key);
                        startActivity(showInfo);
                    }
                });
            }
        };

        mPerson.setAdapter(firebaseRecyclerAdapter);
    }

    public  static class PersonViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public PersonViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }
        public  void setName(String full){
            TextView FullName = (TextView) mView.findViewById(R.id.fName);
            FullName.setText(full);
        }
        public  void setdDate(String dDate){
            TextView Ddate = (TextView) mView.findViewById(R.id.dDate);
            Ddate.setText(dDate);
        }
    }
}

我如何搜索多个孩子?或者我如何添加更多orderByChild?

0 个答案:

没有答案