gpt4 book ai didi

android - populateViewHolder 未使用 FirebaseRecyclerAdapter (Android) 执行

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:21 25 4
gpt4 key购买 nike

我正在尝试使用 FirebaseRecyclerAdapter 使用我的 Firebase 数据库中的数据在 fragment 中填充 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'

这是我 fragment 中的 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);
}
}

Developer 类(模型类):

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

最佳答案

感谢这个文档,我解决了我的问题:

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);
}
}
});

问题解决了!

关于android - populateViewHolder 未使用 FirebaseRecyclerAdapter (Android) 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com