Android - Firebase - Send Users to Chat Room

后端 未结 3 777
余生分开走
余生分开走 2020-12-12 07:27

Aim

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.<

相关标签:
3条回答
  • 2020-12-12 07:46

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

    You also have to update this line in your GroupFragment:

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

    This is another solution that you can implement inside your fragment so that you can put extras in intent (actually modified from your 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);
    }
    

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

    0 讨论(0)
  • 2020-12-12 08:08

    Try according to this

    1. Suppose you've created 5 user's in FirebaseDatabasewith different UID's
    2. In this step you have to get all user's from Firebase and display it in RecyclerView
    3. In Recyclerview's adapter class in onBindViewHolder' you have to do like add and remove` users from list which you generated at the time creating group.
    4. In this step you've to search firebaseDatabase user's Uid which is currently logged in and if your UID is matched found in any Group then you need to get the Group-name .

    Happy to help you

    0 讨论(0)
  • 2020-12-12 08:12
        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);
        }
    
    0 讨论(0)
提交回复
热议问题