Recyclerview没有在android中显示数据

时间:2018-05-29 17:48:51

标签: android firebase firebase-realtime-database android-recyclerview

代码

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_contacts, container, false);

    noUsersText = (TextView) rootView.findViewById(R.id.noUsersText);
    mUsersList = (RecyclerView) rootView.findViewById(R.id.usersList);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    UID = mAuth.getCurrentUser().getUid();
    mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users");


    mProgressDialogue = new ProgressDialog(getActivity());
    mProgressDialogue.setMessage("Loading...");
    mProgressDialogue.show();


    mUsersList.setLayoutManager(new LinearLayoutManager(getActivity()));

    FirebaseRecyclerOptions<AllUsers> options =
            new FirebaseRecyclerOptions.Builder<AllUsers>()
                    .setQuery(mDatabaseReference, AllUsers.class)
                    .build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull AllUsers model) {
            holder.setName(model.getName());
            holder.setStatus(model.getStatus());

            final String userId = getRef(position).getKey();

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Users").child(userId).child("Data");

                    rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            userName = dataSnapshot.child("Name").getValue().toString();
                            Intent intent = new Intent(getActivity(), Chat.class);
                            intent.putExtra("Recievers_Id", userId);
                            intent.putExtra("Recievers_Name", userName);
                            startActivity(intent);
                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
            });
        }
        @Override
        public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.all_users_layout, parent, false);
            return new UsersViewHolder(view);
        }
    };
    mUsersList.setAdapter(firebaseRecyclerAdapter);
    return rootView;
}

public static class UsersViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }
    public void setName(String name) {
        TextView mDisplayName = (TextView) mView.findViewById(R.id.display_name);
        mDisplayName.setText(name);
    }
    public void setStatus(String status) {
        TextView mDisplayStatus = (TextView) mView.findViewById(R.id.display_status);
        mDisplayStatus.setText(status);
    }
}

@Override
public void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}
@Override
public void onStop() {
    super.onStop();
    if(firebaseRecyclerAdapter != null) {
        firebaseRecyclerAdapter.stopListening();
    }
}

}

数据库 - https://ibb.co/kLNqxd

当我的数据库结构为Users/userid/'all the data'时,它运行正常。现在我又添加了一个名为Data的子项,它没有显示数据。有人可以帮帮我吗?

尝试以下2种方法后出错

Process: com.appmaster.akash.messageplus, PID: 15314
                                                                             com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.appmaster.akash.messageplus.HomeScreen_Contacts$UserData
                                                                                 at com.google.android.gms.internal.zzear.zzb(Unknown Source)
                                                                                 at com.google.android.gms.internal.zzear.zza(Unknown Source)
                                                                                 at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                 at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:23)
                                                                                 at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:14)
                                                                                 at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:32)
                                                                                 at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:45)
                                                                                 at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:103)
                                                                                 at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:118)
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
                                                                                 at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                                                                                 at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                                                                                 at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                                                                 at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                                                                 at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                                                                 at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                                                                                 at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                                                                                 at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1630)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
                                                                                 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                 at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                 at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
                                                                                 at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                 at android.view.View.measure(View.java:18827)
                                                                                 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
                                                                                 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                 at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)

2 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(uid).child("Data");

答案 1 :(得分:0)

我最初的猜测是,您需要在班级中Data成员传递给FirebaseRecyclerAdapter。现在这个班级是AllUsers。因此,最简单的更改是为Data创建一个新类:

public class UserData {
  public AllUsers Data;
}

所以这是一个简单的类,只有一个名为Data的公共字段,它匹配您在数据库中添加的Data属性。

使用以下内容将其连接到FirebaseRecyclerAdapter

mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Users");

FirebaseRecyclerOptions<UserData> options =
        new FirebaseRecyclerOptions.Builder<UserData>()
                .setQuery(mDatabaseReference, UserData.class)
                .build();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserData, UsersViewHolder>(options) {

    @Override
    protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull UserData model) {
        holder.setName(model.Data.getName());
        holder.setStatus(model.Data.getStatus());
        ...