Android-Firebase-提示群聊名称 [英] Android - Firebase - Prompting Group Chat Names

查看:55
本文介绍了Android-Firebase-提示群聊名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

群聊已经创建.我想在RecyclerView上提示群聊的名称.

数据库树

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

在数据库不是固定变量中看到的Android,亚洲和欧洲群聊.他们是名字.新的群聊名称可能是"Earth".

因此,除了由Node本身调用之外,没有其他方法可以通过变量调用它.

问题

我无法提示管理员创建的任何群聊名称".意思是,我无法提示"Android",亚洲"和欧洲"或在 Group Chats 节点

中创建和存储的任何Group Chat.

活动流程

AdminActivity --- {创建群组Chat.func}

Users ---- views ----> GroupFragment {提示了GroupChats}

AdminActivity.java-createGroupChat函数

  public void createGroupChat(){Map< String,Object>groupChatMap = new HashMap< String,Object>();groupChatMap.put(jAdminGroupChatName.getText().toString(),");groupChatRoot.updateChildren(groupChatMap);Toast.makeText(AdminActivity.this,已创建群组聊天",Toast.LENGTH_SHORT).show();jAdminGroupChatName.setText(");} 

GroupChats类

 //获取组聊天名称公共课堂GroupChats {公共字符串名称;公开GroupChats(){}公共GroupChats(字符串名称){this.name =名称;}公共字符串getName(){返回名称;}public void setName(String name){this.name =名称;}} 

GroupFragment.java

  @Override公共无效onStart(){super.onStart();FirebaseRecyclerAdapter< GroupChats,GroupChatViewHolder>firebaseRecyclerAdapter =新的FirebaseRecyclerAdapter< GroupChats,GroupChatViewHolder>(GroupChats.class,R.layout.layout_groups,GroupChatViewHolder.class,jGroupsDatabase){@Override受保护的void populateViewHolder(GroupChatViewHolder viewHolder,GroupChats模型,int位置){viewHolder.setName(model.getName());最终的字符串viewGroupID = getRef(position).getKey();viewHolder.jGroupChatView.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view){Intent intentGroupChat = new Intent(getActivity(),ChatFragment.class);intentGroupChat.putExtra("viewGroupID",viewGroupID);startActivity(intentGroupChat);}});}};jGroupChatList.setAdapter(firebaseRecyclerAdapter);}公共静态类GroupChatViewHolder扩展了RecyclerView.ViewHolder {查看jGroupChatView;公共GroupChatViewHolder(查看itemView){超级(itemView);jGroupChatView = itemView;}公共无效setName(String groupName){TextView jAllGroupChatName = jGroupChatView.findViewById(R.id.groupChatNameTxt);jAllGroupChatName.setText(groupName);}}} 

其他评论

  • 在线教程

我已经在youtube上观看了Android Firebase群聊教程.链接为

为此,您应该更新您的 createGroup()函数:

  public void createGroupChat(){字符串newGroupName = jAdminGroupChatName.getText().toString();GroupChats newGroupChat =新的GroupChats(newGroupName);字符串newGroupKey = groupChatRoot.push().getKey();groupChatRoot.child(newGroupKey).setValue(newGroupChat);Toast.makeText(AdminActivity.this,已创建群聊-密钥:" + newGroupKey,Toast.LENGTH_SHORT).show();jAdminGroupChatName.setText(");} 


但是,如果要保留当前格式,则无需使用 FirebaseRecyclerAdapter .您可以在 groupChatRoot 上调用 addValueEventListener()并遍历结果,将键保存到列表中,然后在recyclerview中显示它们,如下所示:

  groupRef.addValueEventListener(new ValueEventListener(){@Override公共无效onDataChange(DataSnapshot dataSnapshot){ArrayList< String>groupChatNames = new ArrayList<>();对于(DataSnapshot子代:dataSnapshot.getChildren()){groupChatNames.add(child.getKey());}适配器适配器=新适配器(groupChatNames);recyclerView.setAdapter(adapter);}@Override公共无效onCancelled(DatabaseError databaseError){}}); 

其中 Adapter 类如下:

 公共类Adapter扩展了RecyclerView.Adapter< Adapter.MyHolder>.{ArrayList< String>列表;公共适配器(ArrayList< String>列表){this.list =列表;}@Overridepublic Adapter.MyHolder onCreateViewHolder(ViewGroup parent,int viewType){视图view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups,parent,false);MyHolder持有人=新的MyHolder(查看);归还持有人;}@Overridepublic void onBindViewHolder(MyHolder持有人,int位置){holder.setText(list.get(position));}@Overridepublic int getItemCount(){返回list.size();}MyHolder类扩展了RecyclerView.ViewHolder {TextView nameTextView;公共MyHolder(查看itemView){超级(itemView);nameTextView =(TextView)itemView.findViewById(R.id.groupChatNameTxt);}公共无效setText(String groupName){nameTextView.setText(groupName);}}} 

Aim

Group Chats has already been created. I would like to prompt out the name of the Group Chats onto a RecyclerView.

Database Tree

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.

Problem

I am unable to prompt out any of the Group Chat "names" created by the Admin. Meaning, I am unable to prompt out the "Android", "Asia", and "Europe" or any Group Chats created and stored within the Group Chats node

Flow of Activities

AdminActivity---{Creates Group Chat.func}

Users----views---->GroupFragment{GroupChats are Prompted}

AdminActivity.java - createGroupChat Function

public void createGroupChat(){
    Map<String, Object> groupChatMap = new HashMap<String, Object>();
    groupChatMap.put(jAdminGroupChatName.getText().toString(),"");
    groupChatRoot.updateChildren(groupChatMap);
    Toast.makeText(AdminActivity.this, "Group Chat Created", Toast.LENGTH_SHORT).show();
    jAdminGroupChatName.setText("");
}

GroupChats Class

// GET GROUP CHAT NAME

public class GroupChats {
    public String name;

    public GroupChats() {
    }

    public GroupChats(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

GroupFragment.java

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

        FirebaseRecyclerAdapter<GroupChats, GroupChatViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<GroupChats, GroupChatViewHolder>(
                GroupChats.class,
                R.layout.layout_groups,
                GroupChatViewHolder.class,
                jGroupsDatabase
        ){
            @Override
            protected void populateViewHolder(GroupChatViewHolder viewHolder, GroupChats model, int position) {
                viewHolder.setName(model.getName());

                final String viewGroupID = getRef(position).getKey();

                viewHolder.jGroupChatView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intentGroupChat =  new Intent(getActivity(), ChatFragment.class);
                        intentGroupChat.putExtra("viewGroupID", viewGroupID);
                        startActivity(intentGroupChat);
                    }
                });

            }
        };

        jGroupChatList.setAdapter(firebaseRecyclerAdapter);

    }

    public static class GroupChatViewHolder extends RecyclerView.ViewHolder{

        View jGroupChatView;

        public GroupChatViewHolder(View itemView) {
            super(itemView);
            jGroupChatView = itemView;
        }

        public void setName(String groupName){
            TextView jAllGroupChatName = jGroupChatView.findViewById(R.id.groupChatNameTxt);
            jAllGroupChatName.setText(groupName);
        }

    }
}

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. In that video, the developer wrote the code in a way which prompts the Group Chat names within the same class which is the MainActivity.java.

Tutorial Flow:

MainActivity----{Creates Group Chat.func}----{Group Chat Prompt.func}

  • Task Requirement

The requirements of my task makes it necessary for me to create it from the AdminActivity and prompt the Group Chat names only for the User's view.

Task Flow:

AdminActivity---{Creates Group Chat.function}

Users----views---->GroupFragment{GroupChats are Prompted}

Future Implementations - Future Question

If I'm able to solve this problem with the help of Stackoverflow users, my future implementation is to allow the Users to click on the "Group Chat" names which will then bring them into the group chat itself, thereby allowing the Email Authenticated(Firebase) users to chat with others inside that Group Chat. Of course, if I am unable to write the codes for that, I will ask for a solution in another Question.

解决方案

Your code would work if the format of "Group Chats" is as follows:

For that, you should update your createGroup() function:

public void createGroupChat(){
    String newGroupName = jAdminGroupChatName.getText().toString();
    GroupChats newGroupChat = new GroupChats(newGroupName);
    String newGroupKey = groupChatRoot.push().getKey();
    groupChatRoot.child(newGroupKey).setValue(newGroupChat);
    Toast.makeText(AdminActivity.this, "Group Chat Created - Key: " + newGroupKey, Toast.LENGTH_SHORT).show();
    jAdminGroupChatName.setText("");
}


However, if you want to keep the current format, then you don't need to use FirebaseRecyclerAdapter. You can call addValueEventListener() on your groupChatRoot and iterate over results, saving keys to a list and then showing them in the recyclerview as follows:

groupRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> groupChatNames = new ArrayList<>();
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            groupChatNames.add(child.getKey());
        }
        Adapter adapter = new Adapter(groupChatNames);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

where Adapter class is as follows:

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

    ArrayList<String> list;

    public Adapter(ArrayList<String> list) {
        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;

        public MyHolder(View itemView) {
            super(itemView);
            nameTextView = (TextView) itemView.findViewById(R.id.groupChatNameTxt);
        }

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

这篇关于Android-Firebase-提示群聊名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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