Android – Firebase – 将用户发送到聊天室
目标
允许用户访问他们选择的群聊。 用户单击群聊名称后,将进入群聊。
数据库树
如数据库树中所示,将显示当前已登录用户的已创建的群聊名称列表。
我有一个管理员帐户来为用户创建这些群聊。
在数据库中看到的Android,亚洲和欧洲组聊天不是固定变量。 他们是名字。 新的群聊名称可以是“地球”。
因此,除了由Node本身调用它之外,没有办法通过变量调用它。
应用截图
- 组聊天列表2.进入群聊
活动流程
-
GroupFragment —>聊天活动
-
GroupFragment <—聊天活动
申请流程
用户—> LoginActivity —> UserActivity —> GroupFrag —> GroupChatActivity
在(GroupFrag —> GroupChatActivity)用户必须在GroupFrag中选择一个群聊名称才能进入GroupChatActivity
用户必须在GroupFrag中选择一个群聊名称才能输入GroupChatActivity
描述
- 用户将能够选择群聊名称(来自GroupFragment),该应用程序将使用户进入群聊(进入聊天活动)。 用户将能够返回GroupFragment并选择另一个所需的组。
(群聊名称未固定 – 它们不是可以从中调用的节点)
问题
- 在片段中提示后,我无法选择群聊名称,这将带我进入群聊。
集团片段
@Override public void onStart() { super.onStart(); class GroupAdapter extends RecyclerView.Adapter { ArrayList list; public GroupAdapter(ArrayList 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 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 。 但它没有提供我试图实现的一些function/function。
- 相关问题
Android – Firebase – 提示群聊名称
未来的实施
对于将来的实现,我想发送和检索消息并将其提示到群聊中,以便用户查看真实的群聊。 但是,当然,我会将其留给另一个问题。
您可以按如下方式更新适配器和查看器实现:
public class Adapter extends RecyclerView.Adapter { Context context; ArrayList list; public Adapter(Context context, ArrayList 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中(实际上是根据您之前的问题进行修改):
@Override public void onStart() { super.onStart(); DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); DatabaseReference groupChatsRef = rootRef.child("Group Chats"); FirebaseRecyclerAdapter chatAdapter = new FirebaseRecyclerAdapter( 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); }
请注意,它将数据库中的字符串字符串组聊天条目作为键值对处理。
public class group_name_list_adapter extends RecyclerView.Adapter { private List< group_name_list> listItems; private Context context; OnItemClickListener onItemClickListener; public group_name_list_adapter(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; private List 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); }
试试这个
- 假设您已使用不同的
UID's
在FirebaseDatabase
创建了5 user's
- 在此步骤中,您必须从
Firebase
get all user's
并将其显示在RecyclerView
- 在
onBindViewHolder' you have to do like
位于onBindViewHolder' you have to do like
Recyclerview's adapter class
中onBindViewHolder' you have to do like
在创建组时生成的列表中添加和删除用户。 - 在此步骤中,您将搜索当前
logged in
firebaseDatabase
用户的Uid
,如果您在任何Group
找到UID is matched
的UID is matched
,则需要获取Group-name
。