0

I am working on Cloud Firestore. My Firestore database document name is users and I want to show all users in Android RecyclerView and my activity name Alluser and I'm using this .XLM file activity_alluser.xml.

I am confused about what to pass parameter inside FirebaseRecyclerAdapter; here is my Java as well as my XML code.

public class Alluser extends AppCompatActivity
{
    private final static String TAG = Alluser.class.getSimpleName();
    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mUsersList;

    private FirebaseAuth mAuth;
    private FirebaseUser mCurrentUser;
    private DocumentReference mUser; 

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

        mUsersList = (RecyclerView)findViewById(R.id.result_list);
        mSearchField = (EditText) findViewById(R.id.search_edittext);
        mUsersList.setHasFixedSize(true);
        mUsersList.setLayoutManager(new LinearLayoutManager(this));


        mAuth = FirebaseAuth.getInstance();
        //String current_user_id = mCurrentUser.getUid();
        String current_user_id = mAuth.getCurrentUser().getUid();
        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();


        mUser = FirebaseFirestore.getInstance().collection("users").document(current_user_id);

        mSearchBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                firebaseUserSearch();
            }
        });
    }

    private void firebaseUserSearch()
    {
        FirebaseRecyclerAdapter<Users,UserviewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UserviewHolder>
                (Users.class,
                        R.layout.single_user_list_layout,
                        UserviewHolder.class,
                        //i am confused what to pass here)
        {
            @Override
            protected void onBindViewHolder(@NonNull UserviewHolder holder, int position, @NonNull Users model) {

            }

            @Override
            public UserviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
            }
        };
        mUsersList.setAdapter(firebaseRecyclerAdapter);
    }

    public class UserviewHolder extends RecyclerView.ViewHolder
    {
        View mView;

        public UserviewHolder(View itemView)
        {
            super(itemView);
            mView = itemView;
        }
        public void setDetails(String username,String useremail,String userimage)
        {
            TextView user_name= (TextView)mView.findViewById(R.id.single_user_name);
            TextView user_email =(TextView)mView.findViewById(R.id.single_user_email);
            CircleImageView user_image = (CircleImageView)mView.findViewById(R.id.circleImageView);

            user_name.setText(username);
            user_email.setText(useremail);
        }
    }
}

XML code:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        android:background="@drawable/fragment_background">


        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="Search All User"
            android:paddingLeft="20sp"
            android:textColor="#000000"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.057"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.031" />

        <EditText
            android:id="@+id/search_edittext"
            android:layout_width="250dp"
            android:layout_height="50dp"
            android:background="@drawable/search_layout"
            android:ems="10"
            android:hint="Enter Email to Search..."
            android:inputType="textEmailAddress"
            android:paddingLeft="20dp"
            android:paddingRight="15dp"
            android:textColor="#999999"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.079"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.149" />

        <ImageButton
            android:id="@+id/search_button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/search_icon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.98"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.149" />

        <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/result_list"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="7dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="7dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="150dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.625">

        </android.support.v7.widget.RecyclerView>

    </android.support.constraint.ConstraintLayout>
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Anurag Tiwari
  • 115
  • 1
  • 10
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 11 '18 at 22:19
  • Looks like I asked you to refrain from begging last week, so I have downvoted this time. – halfer Jan 11 '18 at 22:20
  • For future visitors, you can take a look **[here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)**, where I have explained step by step how to display data from Firestore into a `RecyclerView` using Android. – Alex Mamo Mar 14 '18 at 13:07

1 Answers1

1

In order to make a FirebaseRecyclerAdapter work, you need to pass as the last argument a DatabaseReference, as seen in the official documentation.

So your code should look like this:

FirebaseRecyclerAdapter<Users,UserviewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UserviewHolder>
            (Users.class,
             R.layout.single_user_list_layout,
             UserviewHolder.class,
             mUser)

Beeing an abstract class, you can also take a look a FirebaseRecyclerAdapter class, to see more clearly what method you should override.

Talking about Cloud Firestore, I recomand you using FirestoreRecyclerAdapter instead of FirebaseRecyclerAdapter. To use a this adapter, you'll also need a FirestoreRecyclerOptions in order to make it work.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • How can I use FirestoreRecyclerAdapter?? – Anurag Tiwari Jan 13 '18 at 09:24
  • it doesn't take our DatabaseReference/DocumentReference – Anurag Tiwari Jan 13 '18 at 09:49
  • FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to: Expected Parameters: Actual Arguments: options: com...com.example.mk.digital420.Users> Users.class  (java...com.example.mk.digital420.Users>) R.layout.list_layout  (int) UserviewHolder.class  (java...UsersFragment.UserviewHolder>) mUser  (com...firebase.firestore.DocumentReference) – Anurag Tiwari Jan 13 '18 at 09:55
  • Yes, this is happening because you are using instead of a `DatabaseReference` as I wrote you in my answer, a `DocumentReference` and that's why that error. You cannot mix them. To solve this, use `FirestoreRecyclerAdapter`. As a conclusion, `FirebaseRecyclerAdapter` works perfect with `DatabaseReference` and `FirestoreRecyclerAdapter` works perfect with `DocumentReference`. So do this changes! Do you think that my answer helped you? – Alex Mamo Jan 13 '18 at 10:00
  • Since I am using FireStore So,how i can change DocumentReference into DatabaseReference.? – Anurag Tiwari Jan 13 '18 at 10:12
  • Because you are using Firestore, you need to change the adapter, in order to make it work. Change the FirebaseRecyclerAdapter with **`FirestoreRecyclerAdapter`** so you can use the `DocumentReference` that you already have. Take a look [here](https://github.com/ennur/FirestoreRecyclerAdapterSample) at a sample. This will solve your problem. – Alex Mamo Jan 13 '18 at 10:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163143/discussion-between-anurag-tiwari-and-alex-mamo). – Anurag Tiwari Jan 14 '18 at 13:57
  • No sir, it still giving me an unfortunately stopped error while deploying on the mobile..on that particular Activity – Anurag Tiwari Jan 15 '18 at 06:52
  • Have you tried to use `FirestoreRecyclerAdapter` with `DocumentReference`? You cannot make it work in the other way. – Alex Mamo Jan 15 '18 at 06:55
  • Sir, it gives an Error in 'DocumentReference' while using with 'FirestoreRecyclerAapter'. – Anurag Tiwari Jan 15 '18 at 15:04
  • I post the Changed Code on this link https://stackoverflow.com/questions/48266043/doesnt-show-users-in-recyclerview-while-using-firestorerecycleradapter-and-docu – Anurag Tiwari Jan 15 '18 at 15:34
  • Yes,You helped us alot but still the Error remain with Us..I am surprised that where i done mistake in code which leads me towards a blank Output..! – Anurag Tiwari Jan 15 '18 at 16:43