1

Aim

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

Database Tree

enter image description here

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.

The Employee 123
  • 494
  • 1
  • 14
  • 27
  • Please provide a [mcv](https://stackoverflow.com/help/mcve). There are hundreds of lines of code. Is a little to much for someone to debug online. – Alex Mamo Sep 25 '17 at 19:37
  • You don't have a "name" field in "Group Chats" but your model `GroupChat` has. You should be getting a database exception, do you? – Mehmed Sep 26 '17 at 00:32
  • Yeah you're right @AlexMamo. Sorry about that, I'll edit it soon. – The Employee 123 Sep 26 '17 at 01:18
  • @Mehmed Yeah I don't have a "name" field within the node. At first, I created a child to list all the "Group Chat" names under it but seeing as how the "Group Chat" names aren't fixed, I wasn't able to call it. No exception but I did receive a "No Setter for Android" and "No Setter for Asia" within my Android Monitor. I'll add this into my question. – The Employee 123 Sep 26 '17 at 01:22

1 Answers1

1

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

Correct format

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);
        }
    }
}
Mehmed
  • 2,880
  • 4
  • 41
  • 62
  • Thank you so much @Mehmed!!! It worked!! The Group names were prompted out. I've also tried out your first suggestion in your solution about changing the format. But I think I'll go with your second suggestion =D Thank you again for your help and Have a Nice Day!!! – The Employee 123 Sep 26 '17 at 08:11
  • Hey Mehmed, could you please help me answer this question https://stackoverflow.com/questions/46435225/android-firebase-sending-user-to-chat-room =D – The Employee 123 Sep 27 '17 at 11:47