Android - Firebase - 将用户发送到聊天室 [英] Android - Firebase - Send Users to Chat Room

查看:1049
本文介绍了Android - Firebase - 将用户发送到聊天室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



数据库树





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



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



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



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



应用截图


  1. 组聊天列表2.输入群聊



活动流程


  1. GroupFragment ---> Chat活动


  2. GroupFragment <---聊天活动


申请流程

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

在(GroupFrag ---> GroupChatActivity)用户必须在GroupFrag中选择一个Group Chat名称来进入GroupChatActivity



用户必须在GroupFrag中选择一个Group Chat名称才能进入GroupChatActivity

描述


  1. 用户将能够选择群聊名称从GroupFragment),该应用程序将把用户带到群聊中(进入聊天活动)。用户将能够回到GroupFragment并选择另一个想要的组。

(群聊名称不固定 - 它们不是可以从)



问题


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

组碎片

  @Override 
public void onStart(){
super.onStart();


类GroupAdapter扩展了RecyclerView.Adapter< GroupAdapter.MyHolder> {

ArrayList< String>列表;

public GroupAdapter(ArrayList< String> list){
this.list = list;


$ b @Override
public GroupAdapter.MyHolder onCreateViewHolder(ViewGroup parent,int viewType){
View view = LayoutInflater.from(parent.getContext ())。inflate(R.layout.layout_groups,parent,false);

返回新的MyHolder(view);

$ b @Override
public void onBindViewHolder(MyHolder holder,int position){
holder.setText(list.get(position));


@Override
public int getItemCount(){
return list.size();
}

class MyHolder扩展了RecyclerView.ViewHolder {
TextView nameTextView;
$ b $ public MyHolder(View itemView){
super(itemView);
nameTextView = itemView.findViewById(R.id.groupChatNameTxt);
}

public void setText(String groupName){
nameTextView.setText(groupName);



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

//我不确定在哪里编写userID的代码
//最终字符串usersID = getRef(位置).getKey();
//当用户点击其中一个群聊名称时,他/她将被发送到聊天活动

jGroupChatList.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent intentUserProfile = new Intent(getActivity(),ChatActivity.class);
intentUserProfile.putExtra(groupChatName,groupName);
intentUserProfile.putExtra(neighborhood,neighborhood);
intentUserProfile.putExtra(usersName,usersName);
intentUserProfile.putExtra(usersID,usersID);
startActivity(intentUserProfile);
}
});
}

@Override
public void onCancelled(DatabaseError databaseError){
}
});

聊天活动 b

  @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(neighborhood)。toString();

jChatRoot = FirebaseDatabase.getInstance()。getReference()。child(jGroupChatName);

jChatToolbar =(工具栏)findViewById(R.id.allUSersToolBar);
setSupportActionBar(jChatToolbar);
getSupportActionBar()。setTitle(jGroupChatName); //显示所选组名称的名称
getSupportActionBar()。setDisplayHomeAsUpEnabled(true);

$ b

其他评论




  • 在线教程



在YouTube上的群聊聊天教程。链接是 https://www.youtube.com/watch?v=wVCz1a3ogqk 。但是它并没有提供一些我试图实现的特性/功能。
$ b


  • 链接问题


Android - Firebase - 提示群聊名称



未来实现

为了将来的实现,我想发送和检索消息,并提示它进入群聊,供用户查看,像一个真正的群聊。但是当然,我会把这个问题留给另外一个问题。

解决方案

您可以按照以下步骤更新您的适配器和视图实现: p>

  public class Adapter extends RecyclerView.Adapter< Adapter.MyHolder> {

上下文上下文;
ArrayList< String>列表;

public Adapter(Context context,ArrayList< String> list){
this.context = context;
this.list = list;

$ b @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);
回报持有人;

$ b @Override
public void onBindViewHolder(MyHolder holder,int position){
holder.setText(list.get(position));


@Override
public int getItemCount(){
return list.size();
}

class MyHolder扩展了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);
//如果修正,你应该将这些值传递给适配器的构造函数
// intentUserProfile.putExtra(neighborhood,neighborhood);
// intentUserProfile.putExtra(usersName,usersName);
// intentUserProfile.putExtra(usersID,usersID);
context.startActivity(intentUserProfile);
}
};
$ b $ public MyHolder(View itemView){
super(itemView);
itemView.setOnClickListener(onClickListener);
nameTextView =(TextView)itemView.findViewById(R.id.groupChatNameTxt);
}

public void setText(String groupName){
nameTextView.setText(groupName);



code
$ b

你也必须更新在 GroupFragment

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






这是您可以在您的内部实施的另一个解决方案片段,以便您可以将意外(实际上从您的前问题修改):

$ p $ @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(neighborhood,neighborhood);
intentUserProfile.putExtra(usersName,usersName);
intentUserProfile.putExtra(usersID,usersID);
startActivity(intentUserProfile);
}
});
}
};
jGroupChatList.setAdapter(chatAdapter);





$ b

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


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 Database Tree, the Currently Signed In user will be shown a list of Group Chat names that have been created.

I have an Admin Account to create these Group Chats for the users.

The Android, Asia, and Europe group chats that are seen within the Database ARE NOT fixed variables. They are names. A newly Group Chat name could be "Earth".

Therefore there is no way of calling it by a variable other than calling it by the Node itself.

Screenshot of Application

  1. List of Group Chats 2. Entering a Group Chat

Flow of Activities

  1. GroupFragment ---> Chat Activity

  2. GroupFragment <--- Chat Activity

Flow of Application

User--->LoginActivity--->UserActivity--->GroupFrag--->GroupChatActivity

At the (GroupFrag--->GroupChatActivity) The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity

The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity

Description

  1. The users will be able to select a Group Chat name (from GroupFragment) and the app will bring the user into the Group Chat itself (into Chat Activity). The User will be able to go back to the GroupFragment and select another desired Group.

(Group Chat names are NOT FIXED -- They're not a node that can be called from)

Problem

  1. I am unable to select the Group Chat names after it was prompt within the Fragment, which will then bring me to the Group Chat.

Group Fragment

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

Chat Activity

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

        }

Additional Comments

  • Online Tutorial

I have watched an Android Firebase Group Chat tutorial on youtube. The link is https://www.youtube.com/watch?v=wVCz1a3ogqk. But it does not provide some the features/ functions whic I am trying to implement.

  • Linked Question

Android - Firebase - Prompting Group Chat Names

Future Implementations

For future implementations, I would like to send and retrieve the messages and prompt it into the group chat for the users to view like a real group chat. But of course, I will leave that for another Question.

解决方案

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.

这篇关于Android - Firebase - 将用户发送到聊天室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆