Recyclerview is not showing while fetching from the firebase storage

21 Views Asked by At

i am trying to fetch the data from the firebase storage which is generally some audio file, i just try to fetch into recyclerview but recycler view is not showing. why my recycler view is not showing, please check the code and help me. here is the java activity code :-

 uid = Objects.requireNonNull( FirebaseAuth.getInstance().getCurrentUser() ).getUid();

        DocumentReference documentReference = FirebaseFirestore.getInstance()
                .document("albums/" + uid + "/album_list/" + getIntent().getStringExtra("albumId") );

        documentReference.get().addOnSuccessListener(documentSnapshot -> {

            albumName = (String) documentSnapshot.get("name");
            albumImageUrl = (String) documentSnapshot.get("albumImage");

            Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.setTitle( albumName );
            setSupportActionBar(toolbar);

            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
                actionBar.setHomeAsUpIndicator(R.drawable.back_action_button); // Set your back action button icon
            }

        });

        StorageReference storageReference =  FirebaseStorage.getInstance().getReference()
                .child("album/" + uid + "/" + albumName );

        // List all items (music files) in the "album" folder
        storageReference.listAll().addOnSuccessListener(listResult -> {


            // initialise the list to store the firebase episode
            episodesList = new ArrayList<>();
            long index = 1L;

            for (StorageReference item : listResult.getItems()) {

                String episodeName = item.getName();

                // Get the download URL of the music file
                long finalIndex = index;
                item.getDownloadUrl().addOnSuccessListener(uri -> {

                    String episodeUrl = uri.toString();
                    Episodes episode = new Episodes(finalIndex, episodeName, episodeUrl);
                    episodesList.add(episode);

                    if (episodesList.size() == listResult.getItems().size()){
                        episodesAdapter.notifyDataSetChanged();
                    }

                });

                index++;
            }


            episodesAdapter = new EpisodesAdapter(this, episodesList, albumImageUrl, albumName);
            albumEpisodeRecyclerView =findViewById(R.id.albumEpisodeRecyclerView);
            albumEpisodeRecyclerView.setHasFixedSize(true);
            albumEpisodeRecyclerView.setLayoutManager(new LinearLayoutManager( this ));
            albumEpisodeRecyclerView.setAdapter(episodesAdapter);

        }).addOnFailureListener(e -> {
            // Handle any errors listing the items
            Toast.makeText(this,"loading failed",Toast.LENGTH_SHORT).show();
        });

and this is the adapter and viewholder class :-

public class EpisodesAdapter extends RecyclerView.Adapter<EpisodesAdapter.EpisodesViewHolder> {

    Context context;
    ArrayList<Episodes> episodesList;
    String albumUrl;
    static String albumName;

    public EpisodesAdapter(Context context, ArrayList<Episodes> episodesList, String albumUrl, String albumName) {
        this.context = context;
        this.episodesList = episodesList;
        this.albumUrl = albumUrl;
        EpisodesAdapter.albumName = albumName;
    }

    @NonNull
    @Override
    public EpisodesAdapter.EpisodesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.album_episodes_design_recyclerview,parent,false);
        return new EpisodesAdapter.EpisodesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull EpisodesAdapter.EpisodesViewHolder holder, int position) {

        Episodes episodes = episodesList.get(position);

        holder.episodeNumber.setText( String.valueOf( episodes.getEpisodeNumber() ) );
        holder.episodeName.setText( episodes.getEpisodeName() );

        Glide.with(context)
                .load( albumUrl )
                .centerCrop()
                .into(holder.episodeImageview);

    }

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

    public static class EpisodesViewHolder extends RecyclerView.ViewHolder{

        ImageView episodeImageview,playImageview;
        TextView episodeName,episodeNumber;
        RelativeLayout episode;
        public EpisodesViewHolder(@NonNull View itemView) {
            super(itemView);

            episode = itemView.findViewById(R.id.episode);

            playImageview = itemView.findViewById(R.id.playImageview);
            playImageview.setOnClickListener(v -> {

                String uid = FirebaseAuth.getInstance().getUid();

                if(uid != null){

                    StorageReference storageRef = FirebaseStorage.getInstance().getReference()
                            .child("album/" + uid + "/" + albumName + "/" + episodeName.getText() );

                    storageRef.getDownloadUrl().addOnSuccessListener(uri -> {

                        String episodeUrl = uri.toString();
                        Episodes.playEpisodes(episodeUrl, itemView.getContext() );
                    });
                }
            });

        }
    }
}

and this is my episodes class :-

public class Episodes {
    Long episodeNumber;
    String episodeName,episodeUrl;

    public Episodes(Long episodeNumber, String episodeName, String episodeUrl) {
        this.episodeNumber = episodeNumber;
        this.episodeName = episodeName;
        this.episodeUrl = episodeUrl;
    }

    public static void playEpisodes(String episodeUrl, Context context) {

        ExoPlayer player = new ExoPlayer.Builder(context).build();

        MediaItem mediaItem = MediaItem.fromUri(episodeUrl);    // Build the media item
        player.setMediaItem(mediaItem);   // Set the media item to be played
        player.prepare();   // Prepare the player
        player.play();  // Start the playback

    }

    public Long getEpisodeNumber() {
        return episodeNumber;
    }

    public void setEpisodeNumber(Long episodeNumber) {
        this.episodeNumber = episodeNumber;
    }

    public String getEpisodeName() {
        return episodeName;
    }

    public void setEpisodeName(String episodeName) {
        this.episodeName = episodeName;
    }

    public String getEpisodeUrl() {
        return episodeUrl;
    }

    public void setEpisodeUrl(String episodeUrl) {
        this.episodeUrl = episodeUrl;
    }

}

i just want to show the data which i fetch from the firebase storage that is the musics.

0

There are 0 best solutions below