Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-Firebase数据未显示在ArrayList中_Android_Firebase_Firebase Realtime Database_Android Recyclerview - Fatal编程技术网

Android-Firebase数据未显示在ArrayList中

Android-Firebase数据未显示在ArrayList中,android,firebase,firebase-realtime-database,android-recyclerview,Android,Firebase,Firebase Realtime Database,Android Recyclerview,我正在尝试从Firebase数据库检索数据。顶层节点是消息,子节点是作者和消息。登录后,将从数据库中提取聊天信息ChatActivity调用ChatListAdapter 使用以下代码:- mAdapter = new ChatListAdapter(this, mDatabaseReference); mChatListView.setAdapter(mAdapter); 这里的mChatListView是一个RecyclerView。适配器代码如下所示:- publ

我正在尝试从
Firebase
数据库检索数据。顶层节点是消息,子节点是作者和消息。登录后,将从数据库中提取聊天信息
ChatActivity
调用
ChatListAdapter
使用以下代码:-

    mAdapter = new ChatListAdapter(this, mDatabaseReference);
    mChatListView.setAdapter(mAdapter);
这里的
mChatListView
是一个
RecyclerView
。适配器代码如下所示:-

    public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ViewHolder> {
    private Activity mActivity;
    private DatabaseReference mDatabaseReference;
    private ArrayList<DataSnapshot> mSnapshotList;

    private ChildEventListener mListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            mSnapshotList.add(dataSnapshot);
            notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView authorName, body;

        public ViewHolder(View view) {
            super(view);
            authorName = (TextView) itemView.findViewById(R.id.author);
            body = (TextView) itemView.findViewById(R.id.message);

        }
    }

    public ChatListAdapter(Activity activity, DatabaseReference ref) {
        this.mActivity = activity;
        this.mDatabaseReference = ref.child("messages");
        mDatabaseReference.addChildEventListener(mListener);
        mSnapshotList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chat_row, parent, false);

        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        InstantMessage message = getItem(position);
        String author = message.getAuthor();
        holder.authorName.setText(author);
        String msg = message.getMessage();
        holder.body.setText(msg);
    }

    public InstantMessage getItem(int i) {
        DataSnapshot snapshot = mSnapshotList.get(i);
        return snapshot.getValue(InstantMessage.class);
    }

    @Override
    public int getItemCount() {
        return mSnapshotList.size();
    }    
}

通过映射在数组列表中添加数据,示例推送细节

从Firebase获取数据的以下方法:

public void getUsers(){
    final ArrayList arrayList=new ArrayList();
    DatabaseReference q1=FirebaseDatabase.getInstance().getReference();
    q1.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            collectiouser((Map<String,Object>)dataSnapshot.getValue());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    toolbar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(ChatPage.this,FriendsProfile.class);
            i.putExtra("receiver",receiver);
            startActivity(i);
            finish();
        }
    });

}
public void getUsers(){
最终ArrayList ArrayList=新ArrayList();
DatabaseReference q1=FirebaseDatabase.getInstance().getReference();
q1.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
收集器((Map)dataSnapshot.getValue());
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
toolbar.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent i=新的Intent(ChatPage.this、FriendsProfile.class);
i、 putExtra(“接收者”,接收者);
星触觉(i);
完成();
}
});
}
将数据添加到列表中:

private void collectiouser(Map<String, Object> value) {
    ArrayList<String> user=new ArrayList<>();
    for(Map.Entry<String,Object> entry : value.entrySet()){
        Map singleuser=(Map)entry.getValue();
        String u=entry.getKey();
        String s=singleuser.toString();
        user.add(u); // Add Data into List
    }
}
private void收集器(映射值){
ArrayList用户=新建ArrayList();
for(Map.Entry:value.entrySet()){
Map singleuser=(Map)entry.getValue();
字符串u=entry.getKey();
字符串s=singleuser.toString();
user.add(u);//将数据添加到列表中
}
}
List insightList=new ArrayList();
FirebaseData mFirebaseDatabase=FirebaseDatabase.getInstance().getReference(您的引用键);
mFirebaseDatabase.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
insightList.clear();
对于(DataSnapshot noteSnapshot:DataSnapshot.getChildren(){
Insight note=noteSnapshot.getValue(Insight.class);
见解列表。添加(注释);
}
//recyclerview中的通过列表
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
Log.d(标记,databaseError.getMessage());
}
});

有两种方法可以解决这些问题。是一种推荐的方法,您可以使用
FirebaseRecyclerAdapter
从Firebase实时数据库检索数据并将其显示在
RecyclerView
中。是如何使用
阵列适配器的方法


但请注意,为了使用这些答案,您需要添加Firebase UI依赖项。请查看最新版本。

请发布包含活动的完整代码。@RajshreeTiwari包含活动的完整代码已添加。添加此行
mSnapshotList=new ArrayList();
mSnapshotList.add(dataSnapshot)之前的onchilded内部
@PeterHaddad该行已经存在于ChatListAdapter构造函数中。@John如果您感兴趣,建议您使用
FirebaseRecyclerAdapter
从Firebase实时数据库检索数据,并将其显示在
回收视图中。这是您可以使用
阵列适配器的方式不初始化mSnapshotList,您需要初始化数组列表,如
private ArrayList mSnapshotList=new ArrayList();
I在ChatListAdapter构造函数中进行初始化。在本例中,写入
mAdapter=new ChatListAdapter(此,mDatabaseReference);mChatListView.setAdapter(mAdapter)如果你认为我的答案对你有帮助,请考虑接受它并投票表决。谢谢!如果其中一个答案特别对你有帮助,也请你投赞成票。我很感激。谢谢!
private void collectiouser(Map<String, Object> value) {
    ArrayList<String> user=new ArrayList<>();
    for(Map.Entry<String,Object> entry : value.entrySet()){
        Map singleuser=(Map)entry.getValue();
        String u=entry.getKey();
        String s=singleuser.toString();
        user.add(u); // Add Data into List
    }
}
List<Insight> insightList = new ArrayList<>();
FirebaseData mFirebaseDatabase = FirebaseDatabase.getInstance().getReference(your reference key);
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
         insightList.clear();
         for (DataSnapshot noteSnapshot:dataSnapshot.getChildren(){
               Insight note = noteSnapshot.getValue(Insight.class);
               insightList.add(note);
         }
         // pass list in recyclerview
       }
           @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, databaseError.getMessage());
            }
        });