FirebaseUI + Firebase Database RecyclerView: No adapter attached; skipping layout inside fragment

Viewed 629

I'm trying to combine firebase database, firebase ui and recycler view inside a fragment and I'm getting the 'E/RecyclerView: No adapter attached; skipping layout' error twice.

Could you help me?

Resources

build.gradle

compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.google.firebase:firebase-messaging:11.0.4'
compile 'com.firebaseui:firebase-ui-database:2.2.0'    
compile 'com.android.support:recyclerview-v7:25.3.1'

NotificationFragment

public class NotificationFragment extends Fragment {
    OnListFragmentInteractionListener mListener;
    FirebaseRecyclerAdapter adapter;
    RecyclerView recyclerView;
    public NotificationFragment() {}

    public static NotificationFragment newInstance() {
        NotificationFragment fragment = new NotificationFragment();
        return fragment;
    }


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_notification_list, container, false);

            DatabaseReference ref = FirebaseDatabase.getInstance().getReference()
                    .child("notifications");

            adapter = new FirebaseRecyclerAdapter<FirebaseDBNotification, NotificationViewHolder>(FirebaseDBNotification.class,R.layout.fragment_notification,NotificationViewHolder.class,ref) {
                @Override
                protected void populateViewHolder(NotificationViewHolder viewHolder, FirebaseDBNotification model, int position) {
                    viewHolder.textViewUserId.setText(model.getUserId());
                    viewHolder.textViewLevel.setText(model.getLevel());
                    viewHolder.textViewMsg.setText(model.getMsg());
                    viewHolder.textViewStationPosition.setText(model.getStationPosition());
                    viewHolder.textViewuserPosition.setText(model.getUserPosition());
                }
            };

            if (view instanceof RecyclerView) {
                Context context = view.getContext();
                recyclerView = (RecyclerView) view;
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setAdapter(adapter);
            }

            return view;
        }

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

        @Override
        public void onStop() {
            super.onStop();
            adapter.cleanup();
        }

        public interface OnListFragmentInteractionListener {
            void onListFragmentInteraction(FirebaseDBNotification item);
        }

        public static class NotificationViewHolder extends RecyclerView.ViewHolder {
            TextView textViewUserId,textViewMsg,textViewLevel,textViewStationPosition,textViewuserPosition;

            public NotificationViewHolder(View itemView) {
                super(itemView);
                textViewUserId = itemView.findViewById(R.id.userId);
                textViewMsg = itemView.findViewById(R.id.msg);
                textViewLevel = itemView.findViewById(R.id.level);
                textViewStationPosition = itemView.findViewById(R.id.stationPosition);
                textViewuserPosition = itemView.findViewById(R.id.userPosition);
            }
        }
    }

fragment_notification_list.xml

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/notification_recyclerview"
            android:name="com.uca.ii.pfc.awarenessqualityair.activities.fragments.NotificationFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:visibility="visible"
            tools:listitem="@layout/fragment_notification">
        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

fragment_notification.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/userId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:textAppearance="?attr/textAppearanceListItem" />

main_activity layout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.uca.ii.pfc.awarenessqualityair.activities.AwarenessQualityAirMainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/aqatoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        app:popupTheme="@style/AppThemeSettings"
        android:minHeight="?attr/actionBarSize"
        android:title="@string/awareness_quality_air" />

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.uca.ii.pfc.awarenessqualityair.activities.fragments.NotificationFragment"
        tools:layout="@layout/fragment_notification_list">
    </fragment>

    <LinearLayout
        android:id="@+id/linearLayout"

Loading fragment in main activity

 if (savedInstanceState == null) {
        Fragment details = new NotificationFragment();
        details.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(R.id.listFragment, details).commit();
    }

Firebase Database

Firebase Database schema

Model Object

public class FirebaseDBNotification {
private String userId;
private String msg;
private String level;
private String stationPosition;
private String userPosition;

public FirebaseDBNotification() {}

public FirebaseDBNotification(String userId, String msg, String level, String stationPosition, String userPosition) {
    this.userId = userId;
    this.msg = msg;
    this.level = level;
    this.stationPosition = stationPosition;
    this.userPosition = userPosition;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getLevel() {
    return level;
}

public void setLevel(String level) {
    this.level = level;
}

public String getStationPosition() {
    return stationPosition;
}

public void setStationPosition(String stationPosition) {
    this.stationPosition = stationPosition;
}

public String getUserPosition() {
    return userPosition;
}

public void setUserPosition(String userPosition) {
    this.userPosition = userPosition;
}
}

Thanks in advance.

PD: I tried to use new FirebaseAdapterOptions feature before and getting the same...

1 Answers

The code within the below if block is unreachable because onCreateView() is only called when the fragment is created (to inflate a view for the fragment).

Therefore view will never be an instance of RecyclerView in this context, so the adapter is never being attached to the RecyclerView.

if (view instanceof RecyclerView) {
    Context context = view.getContext();
    recyclerView = (RecyclerView) view;
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(adapter);
}

Try finding the RecyclerView from the inflated view:

recyclerView = (RecyclerView) view.findViewById(R.id.notification_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
recyclerView.setAdapter(adapter);
Related