Skip to content Skip to sidebar Skip to footer

Android - Firebase - Send Users To Chat Room

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. Database Tree As shown in the D

Solution 1:

You can update your adapter and viewholder implementation as follows:

publicclassAdapterextendsRecyclerView.Adapter<Adapter.MyHolder> {

    Context context;
    ArrayList<String> list;

    publicAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    @Overridepublic Adapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Viewview= LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups, parent, false);
        MyHolderholder=newMyHolder(view);
        return holder;
    }

    @OverridepublicvoidonBindViewHolder(MyHolder holder, int position) {
        holder.setText(list.get(position));
    }

    @OverridepublicintgetItemCount() {
        return list.size();
    }

    classMyHolderextendsRecyclerView.ViewHolder {
        TextView nameTextView;
        View.OnClickListeneronClickListener=newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                StringgroupName= list.get(getAdapterPosition());
                IntentintentUserProfile=newIntent(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);
            }
        };

        publicMyHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(onClickListener);
            nameTextView = (TextView) itemView.findViewById(R.id.groupChatNameTxt);
        }

        publicvoidsetText(String groupName) {
            nameTextView.setText(groupName);
        }
    }
}

You also have to update this line in your GroupFragment:

GroupAdapteradapter=newGroupAdapter(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):

@OverridepublicvoidonStart() {
    super.onStart();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference groupChatsRef = rootRef.child("Group Chats");
    FirebaseRecyclerAdapter<String, GroupChatViewHolder> chatAdapter = newFirebaseRecyclerAdapter<String, GroupChatViewHolder>(
            String.class,
            R.layout.layout_groups,
            GroupChatViewHolder.class,
            groupChatsRef) {
        protectedvoidpopulateViewHolder(GroupChatViewHolder viewHolder, String model, int position) {
            final String groupChatName = this.getRef(position).getKey();
            viewHolder.setName(groupChatName);
            viewHolder.itemView.setOnClickListener(newView.OnClickListener() {
                @OverridepublicvoidonClick(View view) {
                    Intent intentUserProfile = newIntent(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.

Solution 2:

publicclassgroup_name_list_adapterextendsRecyclerView.Adapter<group_name_list_adapter.ViewHolder> {

        private List< group_name_list> listItems;
        private Context context;
        OnItemClickListener onItemClickListener;

        publicgroup_name_list_adapter(List<group_name_list> listItems, Context context) {
            this.listItems = listItems;
            this.context = context;
        }

        @Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Viewv= LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.group_name_list, parent, false);
            returnnewViewHolder(v);
        }

        @OverridepublicvoidonBindViewHolder(ViewHolder holder, finalint position) {
            group_name_listlistItem= listItems.get(position);

            holder.txtTitle.setText(listItem.getTxtTitle());
            holder.txtTitle.setOnClickListener(newView.OnClickListener() {
                @OverridepublicvoidonClick(View v) {
                    onItemClickListener.onGroupNameClick(position);
                }
            });
        }

        @OverridepublicintgetItemCount() {
            return listItems.size();
        }

        publicclassViewHolderextendsRecyclerView.ViewHolder {

            public TextView txtTitle;

            publicViewHolder(View itemView) {
                super(itemView);
                txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
            }
        }
        publicvoidsetOnItemClickListener(OnItemClickListener onItemClickListener){
            this.onItemClickListener = onItemClickListener;
        }

        publicinterfaceOnItemClickListener{
            voidonGroupNameClick(int position); 
        }
    }



    publicclassgroup_name_list {

        private String txtTitle;

        publicgroup_name_list(String txtTitle) {
            this.txtTitle = txtTitle;
        }

        public String getTxtTitle() {
            return txtTitle;
        }
    }



    publicclassChatActivityimplementsgroup_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_listnewGroupList= child.getValue();
        );
        groupNameList.add(newGroupList);
    }
    recyclerGroupName.setAdapter(groupNameAdapter);

    gLayoutAttribute = newGridLayoutManager(getActivity(), 1);
    recyclerGroupName = (RecyclerView) rootView.findViewById(R.id.recyclerGroupName);
    recyclerGroupName.setHasFixedSize(true);
    recyclerGroupName.setLayoutManager(newLinearLayoutManager(this.getContext()));
    recyclerGroupName.setLayoutManager(gLayoutAttribute);


@OverridepublicvoidonAttributeClick(int position) {
        IntentintentUserProfile=newIntent(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);
    }

Solution 3:

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

Post a Comment for "Android - Firebase - Send Users To Chat Room"