有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

目标

允许用户访问其选定的群组聊天。一旦用户点击群组聊天名称,他们将进入该群组聊天

数据库树

enter image description here

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

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

在数据库中看到的安卓、亚洲和欧洲群组聊天并不是固定变量。它们是名字。新的群组聊天名称可以是“Earth”

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

应用程序的屏幕截图

  1. 群组聊天列表2。进入群聊

image2image3

活动流程

  1. GroupFragment--->;聊天活动

  2. GroupFragment<;——聊天活动

应用程序流程

用户-->;物流活动--->;用户活动-->;GroupFrag--->;群组聊天活动

在(GroupFrag--->;GroupChatActivity)中,用户必须在GroupFrag中选择一个组聊天名称才能输入GroupChatActivity

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

说明

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

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

问题

  1. 在片段中出现提示后,我无法选择群组聊天名称,这将使我进入群组聊天

组片段

 @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。但是它没有提供我正在尝试实现的一些特性/功能

  • 关联问题

Android - Firebase - Prompting Group Chat Names

未来实施

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


共 (3) 个答案

  1. # 1 楼答案

        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);
        }
    
  2. # 2 楼答案

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

    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);
    

    这是另一个可以在片段内部实现的解决方案,这样您就可以在intent中添加额外的内容(实际上是从former question修改的):

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

    请注意,它将DB中的字符串组聊天条目作为键值对进行处理

  3. # 3 楼答案

    照此试试

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

    很高兴帮助你