populateViewHolder 未使用 FirebaseRecyclerAdapter (Android) 执行
Posted
技术标签:
【中文标题】populateViewHolder 未使用 FirebaseRecyclerAdapter (Android) 执行【英文标题】:populateViewHolder not executing with FirebaseRecyclerAdapter (Android) 【发布时间】:2016-11-10 20:26:14 【问题描述】:我正在尝试使用 FirebaseRecyclerAdapter
在片段中填充 RecyclerView,其中包含来自我的 Firebase 数据库的数据。
我的主要问题是从未调用过 populateViewHolder
方法,因此 RecyclerView
中没有显示任何内容。
我按照Firebase UI
documentation 中的步骤操作:
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.LinearLayout27403c65 V.E..... ......I. 0,0-0,0
D/RECYCLER_VIEW: android.support.v7.widget.RecyclerView23dead3a 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.RecyclerView23dead3a 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
【问题讨论】:
您的 json 结构中有数据吗?在数据库.. 【参考方案1】:感谢这个文档,我解决了我的问题:
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);
);
问题解决了!
【讨论】:
以上是关于populateViewHolder 未使用 FirebaseRecyclerAdapter (Android) 执行的主要内容,如果未能解决你的问题,请参考以下文章
我在 FirebaseRecyclerAdapter 类中找不到 populateViewHolder 方法
Android Studio Logcat 未通过 Wi-Fi 显示带有 ADB 的日志消息
autoreconf -fi 错误:可能未定义宏:AC_DEFINE 如果此令牌和其他令牌是合法的,请使用 m4_pattern_allow