im trying to get data from firestore. but those data don't appears in my fragment activity. and there is this error: E/RecyclerView: No adapter attached; skipping layout
i can only see the background of frangment activy, where is the error?
SecondFragment.class
public class SecondFragment extends Fragment {
public static final String TAG = "FireLog";
RecyclerView mMainList;
FirebaseFirestore mFirestore;
CollectionReference mRef;
View view;
public List<Offices> officesList;
public OfficesListAdapter officesListAdapter;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.second_layout, container, false);
//Recycler View
mMainList = (RecyclerView) view.findViewById(R.id.main_list);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));
mMainList.setAdapter(officesListAdapter);
mFirestore = FirebaseFirestore.getInstance();
officesList = new ArrayList<>();
officesListAdapter = new OfficesListAdapter(officesList);
mFirestore.collection("TICINO").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null){
Log.d(TAG, "Error : " + e.getMessage());
}
for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
if(doc.getType() == DocumentChange.Type.ADDED){
Offices offices = doc.getDocument().toObject(Offices.class);
officesList.add(offices);
officesListAdapter.notifyDataSetChanged();
}
}
}
});
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
OfficeListAdapter.class
public class OfficesListAdapter extends RecyclerView.Adapter<OfficesListAdapter.ViewHolder> {
public List<Offices> officesList;
public OfficesListAdapter(List<Offices> officesList){
this.officesList = officesList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_offices, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.nameText.setText(officesList.get(i).getNameOffices());
viewHolder.cambioText.setText(officesList.get(i).getCambio());
}
@Override
public int getItemCount() {
return officesList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView nameText;
public TextView cambioText;
View mView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView=itemView;
nameText = (TextView) mView.findViewById(R.id.text_view_name_office);
cambioText = (TextView) mView.findViewById(R.id.text_view_cambio_offices);
}
}
}
Offices.class
public class Offices {
String name, cambio;
public Offices(){}
public Offices(String name, String cambio) {
this.name = name;
this.cambio = cambio;
}
public String getNameOffices() {
return name;
}
public void setNameOffices(String name) {
this.name = name;
}
public String getCambio() {
return cambio;
}
public void setCambio(String cambio) {
this.cambio = cambio;
}
}
list_offices.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:cardBackgroundColor="#ffffe8">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/text_view_name_office"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_alignParentLeft="true"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/text_view_cambio_offices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Description"
android:layout_below="@id/text_view_name_office"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>