Android - Firebase - 将用户发送到聊天室

Android - Firebase - Send Users to Chat Room

瞄准

允许用户访问他们的 selected 群聊。一旦用户点击群聊名称,他们将进入该群聊。

数据库树

如数据库树中所示,当前登录的用户将看到已创建的群聊名称列表。

我有一个管理员帐户可以为用户创建这些群聊。

数据库中显示的 Android、亚洲和欧洲群聊不是固定变量。他们是名字。新的群聊名称可以是 "Earth".

因此,除了由节点本身调用外,没有其他方法可以通过变量调用它。

申请截图

  1. 群聊列表2.进入群聊

活动流程

  1. GroupFragment ---> 聊天 Activity

  2. GroupFragment <--- 聊天 Activity

申请流程

用户--->登录Activity--->用户Activity--->GroupFrag--->GroupChatActivity

在(GroupFrag--->GroupChatActivity) 用户必须select GroupFrag 中的群聊名称才能进入GroupChatActivity

用户必须select GroupFrag 中的群聊名称才能进入群聊Activity

描述

  1. 用户将能够 select 群聊名称(来自 GroupFragment),应用会将用户带入群聊本身(进入聊天 Activity)。用户将能够返回到 GroupFragment 和 select 另一个所需的组。

(群聊名称未固定 -- 它们不是可以调用的节点)

问题

  1. 在 Fragment 中出现提示后,我无法 select 群聊名称,然后将我带到群聊。

组片段

 @Override
    public void onStart() {
        super.onStart();


        class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.MyHolder> {

            ArrayList<String> list;

            public GroupAdapter(ArrayList<String> list) {
                this.list = list;
            }


            @Override
            public GroupAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups, parent, false);

                return new MyHolder(view);
            }

            @Override
            public void onBindViewHolder(MyHolder holder, int position) {
                holder.setText(list.get(position));
            }

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

            class MyHolder extends RecyclerView.ViewHolder {
                TextView nameTextView;

                public MyHolder(View itemView) {
                    super(itemView);
                    nameTextView = itemView.findViewById(R.id.groupChatNameTxt);
                }

                public void setText(String groupName) {
                    nameTextView.setText(groupName);
                }
            }
        }

        jGroupsDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ArrayList<String> groupChatNames = new ArrayList<>();
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    groupChatNames.add(child.getKey());
                }
                GroupAdapter adapter = new GroupAdapter(groupChatNames);
                jGroupChatList.setAdapter(adapter);

                //I'm not sure where to write a code for userID
                //final String usersID = getRef(position).getKey();
// When the user clicks on one of the group chat names, he/she will be sent to the Chat Activity

                jGroupChatList.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
                        intentUserProfile.putExtra("groupChatName",groupName);
                        intentUserProfile.putExtra("neighbourhood", neighbourhood);
                        intentUserProfile.putExtra("usersName", usersName);
                        intentUserProfile.putExtra("usersID", usersID);
                        startActivity(intentUserProfile);
                    }
                });
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

聊天Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        jGroupChatName = getIntent().getExtras().get("groupChatName").toString();
        jUserID = getIntent().getExtras().get("usersID").toString();
        jUserName = getIntent().getExtras().get("usersName").toString();
        jUserNeighbourhood = getIntent().getExtras().get("neighbourhood").toString();

        jChatRoot = FirebaseDatabase.getInstance().getReference().child(jGroupChatName);

        jChatToolbar = (Toolbar) findViewById(R.id.allUSersToolBar);
        setSupportActionBar(jChatToolbar);
        getSupportActionBar().setTitle(jGroupChatName); // Show the name of the selected group name
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        }

附加评论

我在 youtube 上观看了 Android Firebase 群聊教程。 link 是 https://www.youtube.com/watch?v=wVCz1a3ogqk。但是它没有提供我正在尝试实现的一些特性/功能。

未来的实施

为了将来的实现,我想发送和检索消息并将其提示到群聊中,供用户像真正的群聊一样查看。但是,当然,我会把它留给另一个问题。

按照这个试试

  1. 假设您在 FirebaseDatabase 中用不同的 UID's
  2. 创建了 5 user's
  3. 在这一步中,您必须从 Firebase get all user's 并将其显示在 RecyclerView
  4. Recyclerview's adapter classonBindViewHolder' you have to do like 从您在创建组时生成的列表中添加和删除用户。
  5. 在这一步中,您必须搜索 firebaseDatabase 用户的 Uid,目前是 logged in,如果您的 UID is matched 在任何 Group 中找到,那么您需要得到 Group-name .

很高兴为您提供帮助

    public class group_name_list_adapter extends RecyclerView.Adapter<group_name_list_adapter.ViewHolder> {

        private List< group_name_list> listItems;
        private Context context;
        OnItemClickListener onItemClickListener;

        public group_name_list_adapter(List<group_name_list> listItems, Context context) {
            this.listItems = listItems;
            this.context = context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.group_name_list, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            group_name_list listItem = listItems.get(position);

            holder.txtTitle.setText(listItem.getTxtTitle());
            holder.txtTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onGroupNameClick(position);
                }
            });
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder {

            public TextView txtTitle;

            public ViewHolder(View itemView) {
                super(itemView);
                txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            }
        }
        public void setOnItemClickListener(OnItemClickListener onItemClickListener){
            this.onItemClickListener = onItemClickListener;
        }

        public interface OnItemClickListener{
            void onGroupNameClick(int position); 
        }
    }



    public class group_name_list {

        private String txtTitle;

        public group_name_list(String txtTitle) {
            this.txtTitle = txtTitle;
        }

        public String getTxtTitle() {
            return txtTitle;
        }
    }



    public class ChatActivity implements group_name_list_adapter.OnItemClickListener

    private RecyclerView recyclerGroupName;
    private group_name_list_adapter groupNameAdapter;
    private List<group_name_list> group_name_List;
    private List<String> groupNameKeyList; //This is optional – this is used if you wanted the group chats to have the same name instead of overwriting the groupchat when creating.

    Inside your Firebase call:

    group_name_List.removeAll(group_name_List t);
    groupNameKeyList.removeAll(groupNameKeyList);
    //Depending on your firebase reference. This could just be dataSnapshot.getChildren()
    for (DataSnapshot child : dataSnapshot.child("Group Chats").getChildren()){


        if (!child.getKey().equals(null)){
            groupNameKeyList.add(child.getKey().toString()); //Again this is optional
        }

        group_name_list newGroupList = child.getValue();
        );
        groupNameList.add(newGroupList);
    }
    recyclerGroupName.setAdapter(groupNameAdapter);

    gLayoutAttribute = new GridLayoutManager(getActivity(), 1);
    recyclerGroupName = (RecyclerView) rootView.findViewById(R.id.recyclerGroupName);
    recyclerGroupName.setHasFixedSize(true);
    recyclerGroupName.setLayoutManager(new LinearLayoutManager(this.getContext()));
    recyclerGroupName.setLayoutManager(gLayoutAttribute);


@Override
    public void onAttributeClick(int position) {
        Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
        intentUserProfile.putExtra("groupChatName",groupName);
        intentUserProfile.putExtra("neighbourhood", neighbourhood);
        intentUserProfile.putExtra("usersName", usersName);
        intentUserProfile.putExtra("usersID", usersID);
        intent.putExtra("name", groupList.get(position).toString());
        //intent.putExtra("name", groupListKeyList.get(position).toString()); this is your optional key
        startActivity(intentUserProfile);
    }

您可以按如下方式更新您的适配器和查看器实现:

public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> {

    Context context;
    ArrayList<String> list;

    public Adapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Adapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups, parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.setText(list.get(position));
    }

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

    class MyHolder extends RecyclerView.ViewHolder {
        TextView nameTextView;
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String groupName = list.get(getAdapterPosition());
                Intent intentUserProfile = new Intent(context, MainActivity.class);
                intentUserProfile.putExtra("groupChatName", groupName);
                // If fixed, you should pass these values to adapter's constructor
                // intentUserProfile.putExtra("neighbourhood", neighbourhood);
                // intentUserProfile.putExtra("usersName", usersName);
                // intentUserProfile.putExtra("usersID", usersID);
                context.startActivity(intentUserProfile);
            }
        };

        public MyHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(onClickListener);
            nameTextView = (TextView) itemView.findViewById(R.id.groupChatNameTxt);
        }

        public void setText(String groupName) {
            nameTextView.setText(groupName);
        }
    }
}

您还必须在 GroupFragment 中更新此行:

GroupAdapter adapter = new GroupAdapter(getActivity(), groupChatNames);

这是您可以在片段中实施的另一种解决方案,以便您可以在意图中添加额外内容(实际上是根据您的 修改的):

@Override
public void onStart() {
    super.onStart();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference groupChatsRef = rootRef.child("Group Chats");
    FirebaseRecyclerAdapter<String, GroupChatViewHolder> chatAdapter = new FirebaseRecyclerAdapter<String, GroupChatViewHolder>(
            String.class,
            R.layout.layout_groups,
            GroupChatViewHolder.class,
            groupChatsRef) {
        protected void populateViewHolder(GroupChatViewHolder viewHolder, String model, int position) {
            final String groupChatName = this.getRef(position).getKey();
            viewHolder.setName(groupChatName);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
                    intentUserProfile.putExtra("groupChatName", groupChatName);
                    intentUserProfile.putExtra("neighbourhood", neighbourhood);
                    intentUserProfile.putExtra("usersName", usersName);
                    intentUserProfile.putExtra("usersID", usersID);
                    startActivity(intentUserProfile);
                }
            });
        }
    };
    jGroupChatList.setAdapter(chatAdapter);
}

请注意,它将您数据库中的字符串-字符串群聊条目作为键值对处理。