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

时间:2017-09-26 20:25:58

标签: java android firebase firebase-realtime-database chat

目标

允许用户访问他们选择的群聊。用户点击群聊名称后,他们就会进入群聊。

数据库树

enter image description here

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

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

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

因此除了由Node本身调用它之外,没有办法通过变量调用它。

应用程序的屏幕截图

  1. 群聊的列表2.进入群聊
  2. image2 image3

    活动流程

    1. GroupFragment --->聊天活动

    2. GroupFragment< --- Chat Activity

    3. 申请流程

      用户---> LoginActivity ---> UserActivity ---> GroupFrag ---> GroupChatActivity

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

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

      描述

      1. 用户可以选择群聊名称(来自GroupFragment),该应用会将用户带入群聊(进入聊天活动)。用户将能够返回GroupFragment并选择另一个所需的组。
      2. (群聊名称未修复 - 他们不是可以从中调用的节点)

        问题

        1. 我无法在片段中提示后选择群聊名称,然后将其带到群聊。
        2. 群组片段

           @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 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

根据此

尝试
  1. 假设您已在FirebaseDatabase中使用不同的UID's
  2. 创建了get all user's
  3. 在此步骤中,您必须Firebase RecyclerView并在Recyclerview's adapter class
  4. 中显示
  5. onBindViewHolder' you have to do like firebaseDatabase中添加和删除您在创建群组时生成的列表中的用户。
  6. 在此步骤中,您可以搜索当前Uid的{​​{1}}用户logged in以及UID is matched Group Group-name然后你需要得到dict
  7. 乐意帮助您