0

i've been working on my application and i want to have a fragment that displays suggested users to follow (display some users from the data base child("users") but when i set up my RecyclerView, no items (profiles) are displayed so heres my layout for the fragment :

enter image description here

the RecyclerView is right under the Suggested Users.. its not showing any data, here's my items XML :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="@dimen/_60sdp"
            android:src="@drawable/profile"
            android:id="@+id/user_in_list_image"
            app:border_color="#fff"
            android:layout_margin="@dimen/_10sdp"
            app:border_width="3dp"
            android:layout_height="@dimen/_60sdp" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:text="User203"
                android:textStyle="bold"
                android:id="@+id/user_in_list_username"
                android:textSize="@dimen/_14sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:text="El Eulma, Algeria"
                android:textSize="@dimen/_12sdp"
                android:id="@+id/user_in_list_info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <com.like.LikeButton
                android:id="@+id/user_in_list_followButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                app:icon_size="@dimen/_16sdp"
                app:is_enabled="true"
                app:icon_type="thumb" />
        </LinearLayout>
        <RelativeLayout
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <com.like.LikeButton
                android:layout_marginRight="@dimen/_30sdp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                app:icon_size="@dimen/_16sdp"
                android:layout_gravity="center|end"
                app:is_enabled="true"
                app:icon_type="heart" />
        </RelativeLayout>

    </LinearLayout>

</FrameLayout>

here's my Object Model java.class :

public class User {
public String name;
public String gender;
public String birthday;
public String work;
public String country;
public String language;
public String city;
public String postal;

public User() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public User(String name ,String gender, String birthday, String work , String country,String language , String city ,String postal) {
    this.name = name;
    this.gender = gender;
    this.birthday = birthday;
    this.work = work;
    this.country = country;
    this.language = language;
    this.city = city;
    this.postal = postal;
}

public String getName() { return name; }

public String getGender() { return gender; }

public String getBirthday() { return birthday; }

public String getWork() { return work; }

public String getCountry() { return country; }

public String getLanguage() { return language; }

public String getCity() { return city; }

public String getPostal() { return postal; }

}

here's my ViewHolder java.class :

public class users_holder extends RecyclerView.ViewHolder{
private static final String TAG = users_holder.class.getSimpleName();
public TextView userName,userInfo;
public CircleImageView userImage;
public com.like.LikeButton likeButton;
public users_holder(View itemView) {
    super(itemView);
    userName= itemView.findViewById(R.id.user_in_list_username);
    userInfo = itemView.findViewById(R.id.user_in_list_info);
    userImage = itemView.findViewById(R.id.user_in_list_image);
    likeButton = itemView.findViewById(R.id.user_in_list_followButton);
}
}

here's my framgent.java onCreateView :

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_discover, container, false);
    if (mListener != null) {
        mListener.onFragmentInteraction("Discover");

    }

    mUser = FirebaseAuth.getInstance().getCurrentUser();
    mUid = mUser.getUid();
    mDatabase = FirebaseDatabase.getInstance();
    mDatabaseref = mDatabase.getReference().child("users");
    suggestedRecycler = view.findViewById(R.id.Suggested_users_recycler);
    mLayoutManager = new LinearLayoutManager(getContext());
    suggestedRecycler.setLayoutManager(mLayoutManager);
    FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
            .setQuery(mDatabaseref, User.class).build();

    FirebaseRecyclerAdapter<User, users_holder> adapter = new FirebaseRecyclerAdapter<User, users_holder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull users_holder holder, int position, @NonNull User model) {
                holder.userName.setText(model.getName());
                holder.userInfo.setText(model.getCity() + ", " + model.getCountry());
        }

        @Override
        public users_holder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.user_in_list, parent, false);

            return new users_holder(view);
        }

    };
    suggestedRecycler.setAdapter(adapter);




    return view;
}

What am i doing wrong ? i saw that other versions were just fine when u had to set up the adapter in an external class, thank you !

Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34

1 Answers1

2

Peter Hadad's answer worked for me here: Android ListView adapter not pushing individual items for chat app - Firebase-Ui 3.1 , so You need to use this:

@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}


@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}

Since FirebaseListAdapter uses a listener to check for changes in the firebase database, then to being listening for data you need to add adapter.startListening() inside the onStart() to be able to show the data in the listview. Then inside onStop(), you can use adapter.stopListening() to remove the listener and the data from the adapter.

Tamim Attafi
  • 2,253
  • 2
  • 17
  • 34