Doesn't show users in RecyclerView while using

2019-09-20 09:19发布

I am using Firestore RecyclerAdapter to display Users in RecyclerView, it still shows a blank page that means it is unable to retrieve the list of users in RecyclerView. I am Confused why it is not displaying the users.

Here is my code:

Java file (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);

            }
     } 

and here is my Users Class (getter/setter file)

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条回答
迷人小祖宗
2楼-- · 2019-09-20 09:47

To solve this, change this:

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

to this:

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

removing rv.setHasFixedSize(true)

also add adapter.startListening() in onStart() and adapter.stopListening() in onStop()

查看更多
登录 后发表回答