populateViewHolder not executing with FirebaseRecy

2019-01-19 19:27发布

问题:

I'm trying to use a FirebaseRecyclerAdapterto populate a RecyclerView in a fragment, with data from my Firebase database.

My main problem is that the populateViewHoldermethod is never called, so I got nothing displaying in the RecyclerView.

I followed the steps from the Firebase UIdocumentation :

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

The rules on my Firebase database :

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

In my gradle, I've added this dependency :

compile 'com.firebaseui:firebase-ui-database:0.4.1'

Here's the onCreateView method in my fragment :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_dev_list, container, false);

    Log.d("ROOT_VIEW", rootView.toString());

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    Log.d("RECYCLER_VIEW", mRecyclerView.toString());

    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    Log.d("ITEM_RECYCLER", Integer.toString(mRecyclerView.getLayoutManager().getItemCount()));

    DatabaseReference devs = FirebaseDatabase.getInstance().getReference().child("users").child("devs");

    FirebaseRecyclerAdapter<Developer, DeveloperViewHolder> adapter =
            new FirebaseRecyclerAdapter<Developer, DeveloperViewHolder>(
                    Developer.class,
                    R.layout.dev_list_row,
                    DeveloperViewHolder.class,
                    devs
            ) {
                @Override
                protected void populateViewHolder(DeveloperViewHolder viewHolder, Developer developer, int position) {
                    Log.d("ON CREATE VIEW", "before");
                    viewHolder.setFirstName(developer.getFirstN());
                    viewHolder.setLastName(developer.getLastN());
                    viewHolder.setAddress(developer.getAddress());
                    Log.d("ON CREATE VIEW", "populate");
                }
            };


    Log.d("COUNT_ADAPTER", Integer.toString(adapter.getItemCount()));
    Log.d("ADAPTER", adapter.toString());
    Log.d("RECYCLER VIEW", mRecyclerView.toString());
    mRecyclerView.setAdapter(adapter);
    Log.d("ON CREATE VIEW", "end");

    return rootView;
} 

And here's my DeveloperViewHolder class :

public static class DeveloperViewHolder extends RecyclerView.ViewHolder {
    TextView firstName;
    TextView lastName;
    TextView address;
    View mView;


    public DeveloperViewHolder(View v) {
        super(v);
        this.mView = v;
    }

    public void setFirstName(String firstN) {
        this.firstName = (TextView) mView.findViewById(R.id.firstname_row);
        this.firstName.setText(firstN);
    }

    public void setLastName(String lastN) {
        this.lastName = (TextView) mView.findViewById(R.id.lastname_row);
        this.lastName.setText(lastN);
    }

    public void setAddress(String addr) {
        this.address = (TextView) mView.findViewById(R.id.address_row);
        this.address.setText(addr);
    }
}

The Developer class (model class) :

public class Developer {

    private String firstN;
    private String lastN;
    private String email;
    private String address;

        public Developer() {

        }

    public Developer(String firstN, String lastN, String email, String address)     {
        this.firstN = firstN;
        this.lastN = lastN;
        this.email = email;
        this.address = address;
    }

    public String getFirstN() {
        return firstN;
    }

    public void setFirstN(String firstN) {
        this.firstN = firstN;
    }

    public String getLastN() {
        return lastN;
    }

    public void setLastN(String lastN) {
        this.lastN = lastN;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

And finally, some logs :

[ 07-08 09:42:42.773 18621:18668 D/         ]
                                                                          HostConnection::get() New Host Connection established 0x7f938cdb65c0, tid 18668
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/ACTION BAR?: android.support.v7.app.ToolbarActionBar@23efcd8a
D/ROOT_VIEW: android.widget.LinearLayout{27403c65 V.E..... ......I. 0,0-0,0}
D/RECYCLER_VIEW: android.support.v7.widget.RecyclerView{23dead3a VFED.V.. ......I. 0,0-0,0 #7f0e00e5 app:id/recycler_view}
D/ITEM_RECYCLER: 0
I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:2 and remote module com.google.android.gms.firebase_database:2
I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 2
D/COUNT_ADAPTER: 0
D/ADAPTER: raphaelgrau.example.com.tabsapp.fragment.tabs.DevListFragment$1@29ec2dc7
D/RECYCLER VIEW: android.support.v7.widget.RecyclerView{23dead3a VFED.V.. ......I. 0,0-0,0 #7f0e00e5 app:id/recycler_view}
D/ON CREATE VIEW: end
E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/raphaelgrau.example.com.tabsapp-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
background_crash     E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.crash.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/raphaelgrau.example.com.tabsapp-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
background_crash W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
background_crash W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
background_crash W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
background_crash W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
background_crash D/ChimeraCfgMgr: Reading stored module config
background_crash I/DynamiteModule: Considering local module com.google.android.gms.crash:0 and remote module com.google.android.gms.crash:2
background_crash I/DynamiteModule: Selected remote version of com.google.android.gms.crash, version >= 2
background_crash D/ChimeraFileApk: Primary ABI of requesting process is x86_64
background_crash I/art: DexFile_isDexOptNeeded failed to open oat file '/data/dalvik-cache/x86_64/data@data@com.google.android.gms@app_chimera@m@00000002@DynamiteModulesC_GmsCore_prodlmp_alldpi_release.apk@classes.dex' for file location '/data/data/com.google.android.gms/app_chimera/m/00000002/DynamiteModulesC_GmsCore_prodlmp_alldpi_release.apk': Failed to open oat filename for reading: No such file or directory
background_crash D/ChimeraFileApk: Classloading successful. Optimized code found.
background_crash I/FirebaseCrashReceiverServiceImpl: FirebaseCrashReceiverServiceImpl created by ClassLoader com.google.android.chimera.container.internal.DelegateLastPathClassLoader[DexPathList[[zip file "/data/data/com.google.android.gms/app_chimera/m/00000002/DynamiteModulesC_GmsCore_prodlmp_alldpi_release.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]]
background_crash D/FirebaseCrashReceiverServiceImpl: onCreate
background_crash I/DynamiteModule: Considering local module com.google.android.gms.flags:0 and remote module com.google.android.gms.flags:1
background_crash I/DynamiteModule: Selected remote version of com.google.android.gms.flags, version >= 1
background_crash I/FirebaseCrashSenderServiceImpl: FirebaseCrashSenderServiceImpl created by ClassLoader com.google.android.chimera.container.internal.DelegateLastPathClassLoader[DexPathList[[zip file "/data/data/com.google.android.gms/app_chimera/m/00000002/DynamiteModulesC_GmsCore_prodlmp_alldpi_release.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]]
background_crash D/FirebaseCrashSenderServiceImpl: onCreate

回答1:

I fixed my problem thanks to this doc :

https://codelabs.developers.google.com/codelabs/firebase-android/#6

I needed to add a RecyclerView.AdapterDataObserver() :

mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
   @Override
   public void onItemRangeInserted(int positionStart, int itemCount) {
       super.onItemRangeInserted(positionStart, itemCount);
       int friendlyMessageCount = mFirebaseAdapter.getItemCount();
       int lastVisiblePosition =
              mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
       // If the recycler view is initially being loaded or the 
       // user is at the bottom of the list, scroll to the bottom 
       // of the list to show the newly added message.
       if (lastVisiblePosition == -1 ||
               (positionStart >= (friendlyMessageCount - 1) &&
                       lastVisiblePosition == (positionStart - 1))) {
           mMessageRecyclerView.scrollToPosition(positionStart);
       }
   }
});

Problem solved!