Android FirebaseRecyclerAdapter populateViewHolder()永远不会被调用 [英] Android FirebaseRecyclerAdapter populateViewHolder() never get called

查看:76
本文介绍了Android FirebaseRecyclerAdapter populateViewHolder()永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的firebase数据结构.我正在尝试使用FirebaseRecyclerAdapter加载此数据.问题是没有调用populateViewHolder().对数据库结构的引用起作用.

I have a firebase data structure like below. I'm trying to load this data with a FirebaseRecyclerAdapter. The issue is that the populateViewHolder() does not get called. The reference to the database structure works.

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

I can read the data with a friendsRef.addValueEventListener. What I like to avoid with the FirebaseRecyclerAdapter.(Code below)

数据结构为:

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

主活动中的身份验证呼叫

Authentication call in the main 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();
                        }
                        // ...
                    }
                });
    }

片段代码:

 @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:

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"

Just change the layout_height of your RecyclerView to "match_parent" in your xml

这篇关于Android FirebaseRecyclerAdapter populateViewHolder()永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆