Java-FirebaseUI RecycleView搜索不起作用

栾钟展
2023-12-01

这是我的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.我要去哪里错了?

编辑:

这是我的数据库结构

更新的代码:

 

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


}

最佳答案

您的此声明:

 

 

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

应该

 

Query firebaseSearchQuery = mUserDatabase.orderByChild("fname").equals(searchText);

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

 类似资料: