Android - Firebase - 将用户发送到聊天室 [英] Android - Firebase - Send Users to Chat Room

查看:33
本文介绍了Android - Firebase - 将用户发送到聊天室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

允许用户访问他们选择的群聊.用户点击群聊名称后,他们将进入该群聊.

Allowing the Users to access their selected Group Chat. Once the user clicks on the Group Chat name, they will be entered into that Group Chat.

数据库树

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

As shown in the Database Tree, the Currently Signed In user will be shown a list of Group Chat names that have been created.

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

I have an Admin Account to create these Group Chats for the users.

在数据库中看到的 Android、亚洲和欧洲群聊不是固定变量.他们是名字.新的群聊名称可以是地球".

The Android, Asia, and Europe group chats that are seen within the Database ARE NOT fixed variables. They are names. A newly Group Chat name could be "Earth".

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

Therefore there is no way of calling it by a variable other than calling it by the Node itself.

应用截图

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

活动流程

  1. GroupFragment ---> 聊天活动

  1. GroupFragment ---> Chat Activity

GroupFragment <--- 聊天活动

GroupFragment <--- Chat Activity

申请流程

用户--->登录活动--->用户活动--->GroupFrag--->GroupChatActivity

User--->LoginActivity--->UserActivity--->GroupFrag--->GroupChatActivity

在(GroupFrag--->GroupChatActivity)用户必须在GroupFrag中选择一个群聊名称才能进入GroupChatActivity

At the (GroupFrag--->GroupChatActivity) The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity

用户必须在 GroupFrag 中选择一个群聊名称才能进入 GroupChatActivity

The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity

说明

  1. 用户将能够选择群聊名称(来自 GroupFragment),应用程序会将用户带入群聊本身(进入聊天活动).用户将能够返回到 GroupFragment 并选择另一个所需的组.

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

(Group Chat names are NOT FIXED -- They're not a node that can be called from)

问题

  1. 在 Fragment 中提示后,我无法选择群聊名称,然后我将进入群聊.

组片段

 @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) {
            }
        });
    }

聊天活动

@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 群聊教程.链接是 https://www.youtube.com/watch?v=wVCz1a3ogqk.但它没有提供我试图实现的一些特性/功能.

I have watched an Android Firebase Group Chat tutorial on youtube. The link is https://www.youtube.com/watch?v=wVCz1a3ogqk. But it does not provide some the features/ functions whic I am trying to implement.

  • 相关问题

Android - Firebase - 提示群聊名称

未来实施

对于未来的实现,我想发送和检索消息并将其提示到群聊中,以便用户像真正的群聊一样查看.但当然,我会把这个留给另一个问题.

For future implementations, I would like to send and retrieve the messages and prompt it into the group chat for the users to view like a real group chat. But of course, I will leave that for another Question.

推荐答案

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

You can update your adapter and viewholder implementation as follows:

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 中更新这一行:

You also have to update this line in your 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);
}

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

Note that it handles your string-string group chat entries in your DB as key-value pairs.

这篇关于Android - Firebase - 将用户发送到聊天室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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