FirebaseUI RecycleView搜索不起作用

时间:2019-02-05 07:45:39

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

这是我的 SearchGetterSetter.java 代码:

package com.me.Admin;

public class SearchGetterSetter {

String fname;

public SearchGetterSetter(String fname) {
    this.fname = fname;
}

public String getFname() {
    return fname;
}

public void setFname(String fname) {
    this.fname = fname;
}
}

这是我的SearchUserActivity.java代码

package com.me.Admin;

  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.TextView;
  import android.widget.Toast;

  import com.firebase.ui.database.FirebaseRecyclerAdapter;
  import com.firebase.ui.database.FirebaseRecyclerOptions;
  import com.google.firebase.FirebaseException;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.Query;
  import com.infoters.killansfitnessstudio.R;
  public class SearchUserActivity extends AppCompatActivity {
private EditText SearchTxt;
private ImageButton SearchBtn;
private RecyclerView SearchRV;

private DatabaseReference mUserDatabase;

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

    mUserDatabase = FirebaseDatabase.getInstance().getReference("users");
    SearchTxt = findViewById(R.id.SearchUserTxt);
    SearchBtn = findViewById(R.id.SearchUserBtn);
    SearchRV = findViewById(R.id.SearchUserRV);
    SearchRV.setHasFixedSize(true);
    SearchRV.setLayoutManager(new LinearLayoutManager(this));


    SearchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String searchText = SearchTxt.getText().toString();
            firebaseUserSearch(searchText);

        }
    });

}

private void firebaseUserSearch(String searchText) {

    Query firebaseSearchQuery = mUserDatabase.child("fname");

    FirebaseRecyclerOptions<SearchGetterSetter> response = new 
FirebaseRecyclerOptions.
            Builder<SearchGetterSetter> 
    ().setQuery(firebaseSearchQuery,SearchGetterSetter.class).build();
    FirebaseRecyclerAdapter<SearchGetterSetter,UserViewHolder> Fire = new 
FirebaseRecyclerAdapter<SearchGetterSetter, UserViewHolder>(response){
        @NonNull
        @Override
        public UserViewHolder onCreateViewHolder(@NonNull ViewGroup 
viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.search_user_item, viewGroup, false);
            return new UserViewHolder(view);
        }
        @Override
        protected void onBindViewHolder(@NonNull UserViewHolder holder, int 
      position, @NonNull SearchGetterSetter model) {
            holder.fName.setText(model.getFname());
        }

    };

    Fire.notifyDataSetChanged();
    SearchRV.setAdapter(Fire);
}

public class UserViewHolder extends RecyclerView.ViewHolder{
    private TextView fName;
    public UserViewHolder(@NonNull View itemView) {
        super(itemView);
        fName = itemView.findViewById(R.id.SUTxt);

    }
}

}

我想从Firebase实时数据库中搜索fname。在搜索按钮上,请按firebaseUserSearch()方法。

我尝试了几种方法,但是对我没有用。我想我做错FirebaseRecycleViewAdapter了。我提到了this。哪里出问题了?

编辑:

这是我的数据库结构Database Stucture

更新的代码:

public class SearchUserActivity extends AppCompatActivity {

private EditText SearchTxt;
private ImageButton SearchBtn;
private RecyclerView SearchRV;

private DatabaseReference mUserDatabase;

private FirebaseRecyclerAdapter<SearchGetterSetter,UserViewHolder> Fire;

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

    mUserDatabase = FirebaseDatabase.getInstance().getReference("users");
    SearchTxt = findViewById(R.id.SearchUserTxt);
    SearchBtn = findViewById(R.id.SearchUserBtn);
    SearchRV = findViewById(R.id.SearchUserRV);
   //        SearchRV.setHasFixedSize(true);
    SearchRV.setLayoutManager(new LinearLayoutManager(this));


    SearchBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String searchText = SearchTxt.getText().toString();
            firebaseUserSearch(searchText);
            Toast.makeText(SearchUserActivity.this, "Clicked", 
        Toast.LENGTH_SHORT).show();

        }
    });

}

private void firebaseUserSearch(String searchText) {

    Query firebaseSearchQuery = mUserDatabase.orderByChild("fname").startAt(searchText).endAt(searchText + "\uf8ff");

    FirebaseRecyclerOptions<SearchGetterSetter> response = new FirebaseRecyclerOptions.
            Builder<SearchGetterSetter>().setQuery(firebaseSearchQuery,SearchGetterSetter.class).build();

    Fire = new FirebaseRecyclerAdapter<SearchGetterSetter, UserViewHolder>(response){
        @NonNull
        @Override
        public UserViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.search_user_item, viewGroup, false);
            return new UserViewHolder(view);
        }
        @Override
        protected void onBindViewHolder(@NonNull UserViewHolder holder, 
       int position, @NonNull SearchGetterSetter model) {
            holder.fname.setText(model.getFname());
        }

    };

    Fire.startListening();
    Fire.notifyDataSetChanged();
    SearchRV.setAdapter(Fire);

}

public static class UserViewHolder extends RecyclerView.ViewHolder{
    private TextView fname;
    public UserViewHolder(@NonNull View itemView) {
        super(itemView);
        fname = (TextView) itemView.findViewById(R.id.SUTxt);
    }
}


}

2 个答案:

答案 0 :(得分:2)

您的声明:

API

应该是

Query firebaseSearchQuery = mUserDatabase.child("fname");

如我所见,您的代码中没有在查询中使用 searchText 变量。

答案 1 :(得分:0)

要创建搜索功能,您需要创建一个如下所示的查询:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
Query query = usersRef.orderByChild("fname").equalTo(searchedText);

因此,每次搜索名称时,都应返回一个新的查询对象。因此,每次您要在新条件下进行过滤时,都需要:

  1. 根据新搜索到的文本创建一个查询:

    Query query = usersRef.orderByChild("name").equalTo(searchedText);
    
  2. 向此新创建的查询附加侦听器。

  3. 使用此新创建的查询的结果创建一个新的适配器,或使用notifydatasetchanged()方法更新现有的适配器。