搜索未检索数据

时间:2020-03-29 06:58:24

标签: android firebase

(我真的需要一些帮助,我已经尝试解决这个问题了好几个星期了,但无济于事)

我想通过用户名从Firebase数据库中搜索用户

但是当我在搜索字段中输入用户名时,它仅显示图像,但不显示用户名或旁边的电子邮件。

这是 SearchActivity 的代码:

package com.example.logindesign;

import android.os.Bundle;
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 androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.squareup.picasso.Picasso;

import de.hdodenhof.circleimageview.CircleImageView;

public class SearchActivity extends AppCompatActivity {

    private EditText mSearchField;

    private RecyclerView mResultList;
     DatabaseReference mUserDatabase;


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

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


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

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

        mSearchBtn.setOnClickListener(view -> {
            String searchText = mSearchField.getText().toString();

            firebaseUserSearch(searchText);

        });

    }

    private void firebaseUserSearch(String searchText) {

        Toast.makeText(SearchActivity.this, "Started Search", Toast.LENGTH_LONG).show();

        Query query = mUserDatabase.orderByChild("username").startAt(searchText).endAt(searchText + "\uf8ff");

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

        FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
                holder.username.setText(model.getUsername());
                holder.email.setText(model.getEmail());
                Picasso.get().load(model.getImage()).into(holder.image);

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

                return new UsersViewHolder(view);
            }
        };

        mResultList.setAdapter(firebaseRecyclerAdapter);

        firebaseRecyclerAdapter.startListening();


    }

    // View Holder Class

    public static class UsersViewHolder extends RecyclerView.ViewHolder {


        TextView username, email;
        CircleImageView image;


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

            username = itemView.findViewById(R.id.name_text);
            email = itemView.findViewById(R.id.status_text);
            image = itemView.findViewById(R.id.profile_image);
        }
    }
}

这是 UserActivity:

package com.example.logindesign;

/**
 * Created by AkshayeJH on 15/12/17.
 */

public class Users {

    public String username, email, image;

    public Users(){

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getImage() {
        return image;
    }

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

    public Users(String username, String email, String image) {
        this.username = username;
        this.email = email;
        this.image = image;
    }
}

结果: enter image description here

1 个答案:

答案 0 :(得分:0)

结果是在XML文件中,我必须添加CardView并在其中包含文本视图才能获得结果