我正在尝试使用FirebaseRecyclerAdapter
使用Firebase数据库中的数据填充片段中的RecyclerView。
我的主要问题是永远不会调用populateViewHolder
方法,因此RecyclerView
中没有显示任何内容。
我按照Firebase UI
文档中的步骤进行了操作:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
我的Firebase数据库中的规则:
{
"rules": {
".read": "true",
".write": "true"
}
}
在我的 gradle 中,我添加了此依赖项:
compile 'com.firebaseui:firebase-ui-database:0.4.1'
这是片段中的 onCreateView 方法:
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;
}
这是我的 DeveloperViewHolder 类:
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);
}
}
开发人员类(模型类):
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;
}
}
最后,一些记录:
[ 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
答案 0 :(得分:4)
由于这个文档,我解决了我的问题:
https://codelabs.developers.google.com/codelabs/firebase-android/#6
我需要添加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);
}
}
});
问题解决了!