Skip to content Skip to sidebar Skip to footer

Android - Firebase - Prompting Group Chat Names

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 C

Solution 1:

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

Correct format

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

publicvoidcreateGroupChat(){
    StringnewGroupName= jAdminGroupChatName.getText().toString();
    GroupChatsnewGroupChat=newGroupChats(newGroupName);
    StringnewGroupKey= 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(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        ArrayList<String> groupChatNames = newArrayList<>();
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            groupChatNames.add(child.getKey());
        }
        Adapter adapter = newAdapter(groupChatNames);
        recyclerView.setAdapter(adapter);
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {

    }
});

where Adapter class is as follows:

publicclassAdapterextendsRecyclerView.Adapter<Adapter.MyHolder> {

    ArrayList<String> list;

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

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

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

Post a Comment for "Android - Firebase - Prompting Group Chat Names"