在Android中使用FirestoreRecyclerAdapter时,不会在RecyclerView中显示用户

时间:2018-01-13 20:38:14

标签: android firebase google-cloud-firestore

我使用Firestore RecyclerAdapter在RecyclerView中显示用户,它仍然显示一个空白页面,这意味着它无法检索RecyclerView中的用户列表。我很困惑,为什么它没有显示用户。

这是我的代码:

Java文件(MainActivity.java)

public class UserSearch extends AppCompatActivity 
{


    private RecyclerView rv;
    private ProgressBar mProgressbar;
    private CollectionReference mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_search);
        rv = (RecyclerView) findViewById(R.id.myrecycler_view);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this));
        mUser = FirebaseFirestore.getInstance().collection("users");
        Query query = mUser;
        FirestoreRecyclerOptions<Users> options = new    
        FirestoreRecyclerOptions.Builder<Users>()
                .setQuery(query, Users.class)
                .build();

        FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Users,   
        FriendsHolder>(options)
        {

            @Override
            public FriendsHolder onCreateViewHolder(ViewGroup group, int i) 
            {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.list_layout, group, false);
                return new FriendsHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) 
            {
                Log.e("error", e.getMessage());
            }

            @Override
            protected void onBindViewHolder(FriendsHolder holder, int  
            position,users model) 
            {
                holder.nameText.setText(model.getName());
                holder.emailText.setText(model.getEmail());
            }

        };
        rv.setAdapter(adapter);

    }   
    private class FriendsHolder extends RecyclerView.ViewHolder 
    {
            View  mView;
            public TextView nameText;
            public TextView emailText;

            public FriendsHolder(View itemview) 
            {
                super(itemview);
                mView=itemview;
                nameText=(TextView)mView.findViewById(R.id.single_user_name);
                emailText=(TextView)mView.findViewById(R.id.Email_user);

            }
     } 

这是我的用户类(getter / setter文件)

public class Users {
    public String Name;
    public String Email;

    public Users(){

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }


    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public Users(String name, String email) {
      this.Name = name;
       this.Email = email;
    }
}

1 个答案:

答案 0 :(得分:0)

要解决此问题,请更改此信息:

  setContentView(R.layout.activity_user_search);
    rv = (RecyclerView) findViewById(R.id.myrecycler_view);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this));
    mUser = FirebaseFirestore.getInstance().collection("users");
    Query query = mUser;
    FirestoreRecyclerOptions<Users> options = new    
    FirestoreRecyclerOptions.Builder<Users>()
            .setQuery(query, Users.class)
            .build();

到此:

     setContentView(R.layout.activity_user_search);
    rv = (RecyclerView) findViewById(R.id.myrecycler_view);
    rv.setLayoutManager(new LinearLayoutManager(this));
    mUser = FirebaseFirestore.getInstance().collection("users");
    Query query = mUser;
    FirestoreRecyclerOptions<Users> options = new    
    FirestoreRecyclerOptions.Builder<Users>()
            .setQuery(query, Users.class)
            .build();

删除rv.setHasFixedSize(true)

还在adapter.startListening()onStart()adapter.stopListening()添加onStop()