Handelika
Handelika

Reputation: 382

How can i fill the recylerview with firebase realtime database data?

I tried so many solutions about this, but none of this resolve my problem. I can not reach the database using recyclerview. I have firebase realtime database data like this;

Tag --- Main Category ----- keyID ------ main-Category, sub-category

Tags class

public class Tags {
    private String id;
    private String mainTags;
    private String subTags;

    public Tags() {}

    public Tags(String id, String mainTags, String subTags) {
        this.id = id;
        this.mainTags = mainTags;
        this.subTags = subTags;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMainTags() {
        return mainTags;
    }

    public void setMainTags(String mainTags) {
        this.mainTags = mainTags;
    }

    public String getSubTags() {
        return subTags;
    }

    public void setSubTags(String subTags) {
        this.subTags = subTags;
    }
}


and this is TagAdapter

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.ItemViewHolder> {

    private List<Tags> mTagList;
    private Context mContext;

    public TagAdapter(List<Tags> mTagList, Context mContext) {
        this.mTagList = mTagList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item_tags, parent,false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {


        Tags tags = mTagList.get(position);
        holder.tvTags.setText(tags.getSubTags());
        holder.tvMainTags.setText(tags.getMainTags());

    }

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


    public class ItemViewHolder extends RecyclerView.ViewHolder{

        private TextView tvMainTags, tvTags;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);

            tvMainTags = itemView.findViewById(R.id.tvMainTag);
            tvTags = itemView.findViewById(R.id.tvTags);
        }
    }
}

TagsFragment

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tags, container, false);
        rvTags = view.findViewById(R.id.rvTags);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTags.setLayoutManager(linearLayoutManager);
        rvTags.hasFixedSize();

        tagAdapter = new TagAdapter(tagsList, getContext());
        rvTags.setAdapter(tagAdapter);


        return view;
    }

private void getTagView(){
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference dbRefTags = firebaseDatabase.getReference().child("Tag");

        dbRefTags.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                for (DataSnapshot ds : dataSnapshot.getChildren()){
                    if (ds.exists()) {
                        try {
                            Tags tags = ds.getValue(Tags.class);
                            tagsList.add(tags);
                        }catch (Exception e){
                            Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                        }

                    }else {
                        Toast.makeText(getContext(), "Bir hata oluştu", Toast.LENGTH_SHORT).show();
                    }
                }

                tagAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

}


There is no error show up when running but i couldn't be able to retrieve data. And i tried firebaseRecyclerView.Adapter also and in the last dependency, i couldn't make it work. I don't know what is wrong in here. how can i solve this?

Upvotes: 1

Views: 657

Answers (1)

shb
shb

Reputation: 6277

Get The data with child name ds.child("SubTag").getValue()

  dbRefTags.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot parentDS : dataSnapshot.getChildren()) {
                        Log.d("Tag:", String.valueOf(parentDS.getKey()));
                        for (DataSnapshot ds : parentDS.getChildren()) {
                            Tags tags = new Tags();
                            tags.setMainTag(parentDS.getKey());
                            tags.setSubTag(ds.child("SubTag").getValue().toString());
                            Log.d("Tag: -> SubTag)", tags.getSubTag());
                            tagsList.add(tags);
                        }
                    }
                    tagAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

Upvotes: 1

Related Questions