gpt4 book ai didi

Android FirebaseRecyclerAdapter populateViewHolder() 永远不会被调用

转载 作者:太空狗 更新时间:2023-10-29 14:47:55 25 4
gpt4 key购买 nike

我有一个如下所示的 Firebase 数据结构。我正在尝试使用 FirebaseRecyclerAdapter 加载此数据。问题是 populateViewHolder() 没有被调用。对数据库结构的引用有效。

我可以使用 friendsRef.addValueEventListener 读取数据。我想用 FirebaseRecyclerAdapter 避免什么。(下面的代码)

数据结构为:

-users  
|-<uid>
|-friends
|-<uid>:name1
|-<uid>:name2

在主 Activity 中调用身份验证

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(getString(R.string.logtag), "signInWithCredential:onComplete:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(HomeActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}

fragment 代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.friends_fragment, container, false);
RecyclerView friendRecyclerView = (RecyclerView) view.findViewById(R.id.friends_recycler_view);

friendRecyclerView.setHasFixedSize(true);
friendRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));

DatabaseReference friendsRef = mActivity.getRootRef().child("users").child(mActivity.getAuth().getCurrentUser().getUid()).child("friends");

FirebaseRecyclerAdapter<String, ViewHolder> recyclerViewAdapter = new FirebaseRecyclerAdapter<String, ViewHolder>(String.class, R.layout.friends_list_item, ViewHolder.class, friendsRef) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, String model, int position) {
viewHolder.friendName.setText(model);
}
} ;

friendRecyclerView.setAdapter(recyclerViewAdapter);

return view;
}

@Override
protected int getContentView() {
return R.layout.friends_fragment;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView friendName;
public ViewHolder(View itemView) {
super(itemView);
friendName=(TextView) itemView.findViewById(R.id.friends_name_list_item);
}


}

friendsRef.addValueEventListener:

DatabaseReference friendsRef = mActivity.getRootRef().child("users").child(mActivity.getAuth().getCurrentUser().getUid()).child("friends");

friendsRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot data : dataSnapshot.getChildren()) {

Log.d("tag", data.toString());
}


}

最佳答案

只需在 xml 中将 RecyclerView 的 layout_height 更改为“match_parent”

关于Android FirebaseRecyclerAdapter populateViewHolder() 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37876904/

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