无法从Firebase实时数据库获取数据

时间:2020-01-30 14:14:00

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

我正在制作一个应用程序,在主屏幕上有一个“编辑文本”和一个固定在“编辑文本”下方的RecyclerView。屏幕上也有一个搜索按钮。我想制作一个Funcion,就像用户在“编辑文本”中输入一些值,以及当用户单击“搜索”按钮添加完该值之后,然后从Firebase检索值并在“回收者视图”中显示。这意味着我想使用Firebase进行搜索。因此,我为此编写了代码,但是问题是我没有从Firebase获得价值。当我单击搜索按钮时,我的回收站视图仍为空白。

我在下面提供了我的代码,我想进行完整的搜索视图。

这是我的代码:-

public class SearchFactorActivity extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchBtn;

    private RecyclerView mResultList;

    private DatabaseReference mUserDatabase;

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

        mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");


        mSearchField = (EditText) findViewById(R.id.search_field);
        mSearchBtn = (ImageButton) findViewById(R.id.search_btn);

        mResultList = (RecyclerView) findViewById(R.id.result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String searchText = mSearchField.getText().toString();

                firebaseUserSearch(searchText);

            }
        });

    }

    private void firebaseUserSearch(String searchText) {

        Query firebaseSearchQuery  = mUserDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "/uf8ff");

        FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>().setQuery(firebaseSearchQuery ,Users.class)
                .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Users , UsersViewHolder>(options) {

            @NonNull
            @Override
            public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout , parent , false);
                return new UsersViewHolder(v);
            }

            @Override
            protected void onBindViewHolder(@NonNull UsersViewHolder usersViewHolder, int i, @NonNull Users users) {

                usersViewHolder.setDetails(getApplicationContext() , users.getName()  , users.getStatus() , users.getImage());

            }
        };

        mResultList.setAdapter(adapter);

    }


    // View Holder Class

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public UsersViewHolder(View itemView) {
            super(itemView);

            mView = itemView;

        }

        public void setDetails(Context ctx, String userName, String userStatus, String userImage){

            TextView user_name = (TextView) mView.findViewById(R.id.name_text);
            TextView user_status = (TextView) mView.findViewById(R.id.status_text);
            ImageView user_image = (ImageView) mView.findViewById(R.id.profile_image);


            user_name.setText(userName);
            user_status.setText(userStatus);

            Glide.with(ctx).load(userImage).into(user_image);


        }




    }

}

这是我的模态课:-

public class Users {

    public String name, image, status;

    public Users(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Users(String name, String image, String status) {
        this.name = name;
        this.image = image;
        this.status = status;
    }
}

我的Firebase结构:-

enter image description here

因此,如果您有任何解决方案,请帮助我。

0 个答案:

没有答案