Firebase:如何在整个数据库或嵌套数据库中搜索具有相同值的所有对象

时间:2019-03-24 05:21:21

标签: java android firebase search firebase-realtime-database

如何从嵌套数据库中获取具有相同name_mov值的所有对象。我有一个EditText,从那里我可以得到一个字符串,用于从嵌套数据库中搜索具有相同mov_name值的对象。我想在Firebase-UI的bolly/new中显示bolly/popular下的所有对象的列表,还显示mov_name中具有相同RecyclerView值的@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); progressBar = findViewById(R.id.spin_search); progressBar.setVisibility(View.GONE); recyclerView = findViewById(R.id.rec_search); recyclerView.setLayoutManager(new LinearLayoutManager(this)); editText = findViewById(R.id.ed_search); editText.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) { firebaseSearch(s.toString()); } @Override public void afterTextChanged(Editable s) { } }); mFirebaseDatabase = FirebaseDatabase.getInstance(); mDatabaseReference = mFirebaseDatabase.getReference(); } //search data private void firebaseSearch(String s) { /*Toast.makeText(AllUserActivity.this, "Started Search", Toast.LENGTH_LONG).show();*/ progressBar.setVisibility(View.VISIBLE); Query firebaseSearchQuery = mDatabaseReference.equalTo(s); //set Options FirebaseRecyclerOptions<MovieObj> options = new FirebaseRecyclerOptions.Builder<MovieObj>() .setQuery(firebaseSearchQuery, MovieObj.class) .setLifecycleOwner(this) .build(); adapter = new FirebaseRecyclerAdapter<MovieObj, SearchMovHolder>(options) { @Override protected void onBindViewHolder(@NonNull SearchMovHolder holder, int position, @NonNull MovieObj model) { progressBar.setVisibility(View.GONE); holder.name.setText(model.getName_mov()); holder.rating.setText( model.getRating()); holder.year.setText( model.getYear()); Glide.with(holder.img_back.getContext()) .load(model.getImgback()) .into(holder.img_back); } @NonNull @Override public SearchMovHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_mov_item, parent, false); return new SearchMovHolder(mView); } }; recyclerView.setAdapter(adapter); }

这是数据库结构:

enter image description here

我做了什么-

<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<div id="main">
<h1 id="title">Survey Form</h1>
<p id="description">Favourite Wu-Tang Things</p>

  <div class="leftsidequestions">
      <form id="survey-form">
       <label id="name-label" for="name">Name:</label>
      <input type="name" id="name" required placeholder="Name"><br>
      <br>
        <label id="email-label" for="email">Email:</label> 
        <input type="email" id="email" required  placeholder="Email"><br>
      <br>
        <label id="number-label" for="age">&ensp; Age: </label>
        <input type="number" id="number" min="1" max="100"  placeholder="Age"><br><br>
        Favourite Wu-Tang Album: 
        <select id="dropdown">
        <option value="1">Enter the Wu-Tang (36 Chambers)</option>
        <option value="2">Wu-Tang Forever</option>
        <option value="3">The W</option>
        <option value="4">Iron Flag</option>
        <option value="5">8 Diagrams</option>
        <option value="6">A Better Tomorrow</option>
        <option value="7">Once Upon a Time in Shaolin</option>
        </select><br><br>
        Favourite Member<br>
        <div class="inputs"><input type="radio" name="member" value"rza">Rza
        <br><input type="radio" name="member" value"gza">Gza
        <br><input type="radio" name="member" value"odb">Ol Dirty Bastard
        <br><input type="radio" name="member" value"Methodman">Method-Man<br>
        <input type="radio" name="member" value"Raekwon">Raekwon
        <br><input type="radio" name="member" value"U-God">U-God
        <br><input type="radio" name="member" value"Masta">Masta Killa
        <br><input type="radio" name="member" value"Ghostface">Ghostface Killah
        </div>
  </div>
</form>
</div>

1 个答案:

答案 0 :(得分:1)

您无法使用数据库结构来实现此目的,因为在根节点和包含name_mov属性的对象之间有4个孩子,movbolly,{ {1}}和推送的ID。

您可以使用以下查询:

new

但是您只能获得Query firebaseSearchQuery = mDatabaseReference .child("mov") .child("bolly") .child("new") .orderByChild("name_mov") .equalTo(s); 节点中存在的电影标题。如果要获取所有标题,则应通过添加电影的类型(新电影,热门电影等)作为对象的属性来重构数据库,然后只需使用:

new

这样,您的级别将减少,查询将正常运行。