-2

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>
Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

0

Create your adapter object before set it on RecyclerView

 // add below method in your adapter class
    public void dataChanged(List<Offices> officesList){
    this.officesList = officesList;
    notifyDataSetChanged();// make sure dont forget to call this method
}


 // create adapter object before set it on RecyclerView
 LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));
officesListAdapter = new OfficesListAdapter(null);// add this line here
mMainList.setAdapter(officesListAdapter);
mFirestore = FirebaseFirestore.getInstance();
officesList = new ArrayList<>();


 // and finally update data on your adapter from your firebase onEvent() method
   for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
            if(doc.getType() == DocumentChange.Type.ADDED){
                Offices offices = doc.getDocument().toObject(Offices.class);
                officesList.add(offices);

            }
        }
        officesListAdapter.dataChanged(officesList);// update from here
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
0

Try to set the adapter after instantiating it. E.g.:

// Inside OnCreate
//Recycler View
mMainList = (RecyclerView) view.findViewById(R.id.main_list);

LinearLayoutManager manager = new LinearLayoutManager(getContext());
mMainList.setHasFixedSize(true);
mMainList.setLayoutManager(new LinearLayoutManager(getContext()));

mFirestore = FirebaseFirestore.getInstance();

officesList = new ArrayList<>();
officesListAdapter = new OfficesListAdapter(officesList);

// Here your adapter is initialised
mMainList.setAdapter(officesListAdapter);
Entreco
  • 12,738
  • 8
  • 75
  • 95
  • tried but i this error now E/AndroidRuntime: FATAL EXCEPTION: main Process: com.galbusera.swisschange, PID: 18905 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference at com.galbusera.swisschange.OfficesListAdapter.getItemCount(OfficesListAdapter.java:43) – Andrea Galbusera Jun 02 '19 at 13:02
  • Thanks, and now? aahha :) – Andrea Galbusera Jun 02 '19 at 13:11