简体   繁体   中英

Getting a problem in sending and adding recycler-view data in an activity

I was trying to develop a music app ,so I have two activities like ArtistActivity and FavoriteActivity . My ArtistActivity contains a RecyclerView which loads SongName , SongUrl , SongArtist in a ListView and it loads data from Firebase, and for each item there is a heart bang icon in the end of the item. My main question is when I click item, then I want to send clicked item in other activity which was my FavoriteActivity and I want to play the song when item is clicked on FavoriteActivity . So for this I need to manage SongUrl in FavoriteActivity and I am confused on this topic.

For example :

When we click Add to favorite in some apps and you can take example of Spotify in my app. I want to build same function and i provided my code below. So please if you have any answer regarding my question please answer me.

Here is my code :

My Adapter Class :

package com.geetmp3.GeetMp3.ArtistPanel;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geetmp3.GeetMp3.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import xyz.hanks.library.bang.SmallBangView;

public class PanelAdapter extends RecyclerView.Adapter<PanelAdapter.PanelViewHolder> {


    private Context mContext;
    private List<PanelList> mUploads;


    public PanelAdapter(Context context, List<PanelList> panelList) {

        mContext = context;
        mUploads = panelList;

    }

    @NonNull
    @Override
    public PanelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.artist_recycler_layout, parent, false);
        return new PanelViewHolder(v);

        // Toast.makeText(mContext, "" + ge, Toast.LENGTH_SHORT).show();

    }

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

        PanelList panelList = mUploads.get(position);


        holder.textViewName.setText(panelList.getSongName());

        Glide.with(mContext)
                .load(panelList.getSongImageUri())
                .centerCrop()
                .into(holder.circleImageView);
//       

        holder.bangView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.bangView.isSelected()) {
                    holder.bangView.setSelected(false);

                    // What can i do here 


                } else {
                    holder.bangView.setSelected(true);
                    holder.bangView.likeAnimation(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            Toast.makeText(mContext, mUploads.get(position).getSongName() + "Added to playlist", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });


    }

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

    public class PanelViewHolder extends RecyclerView.ViewHolder {

        private TextView textViewName;
        private CircleImageView circleImageView;
        private LinearLayout thisLayout;

        private SmallBangView bangView;


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

            thisLayout = itemView.findViewById(R.id.artist_linear);
            textViewName = itemView.findViewById(R.id.artist_song_name);
            circleImageView = itemView.findViewById(R.id.artist_songs_image);
            bangView = itemView.findViewById(R.id.bang_like_heart);


        }

    }
}

My getter and setter class:

public class PanelList {


        private String SongName;
        private String SongUrl;
        private String SongImageUri;
        private String SongLyrics;
        private String SongMusicDirector;
        private String SongProducer;
        private String SongArtist;

        public PanelList() {

        }

        public PanelList(String SongName, String SongUrl, String SongImageUri, String SongArtist, String SongLyrics, String SongMusicDirector, String SongProducer) {
            this.SongName = SongName;
            this.SongUrl = SongUrl;
            this.SongImageUri = SongImageUri;
            this.SongArtist = SongArtist;
            this.SongLyrics = SongLyrics;
            this.SongMusicDirector = SongMusicDirector;
            this.SongProducer = SongProducer;
        }


        public String getSongArtist() {
            return SongArtist;
        }


        public String getSongLyrics() {
            return SongLyrics;
        }


        public String getSongMusicDirector() {
            return SongMusicDirector;
        }


        public String getSongProducer() {
            return SongProducer;
        }


        public String getSongName() {
            return SongName;
        }


        public String getSongUrl() {
            return SongUrl;
        }


        public String getSongImageUri() {
            return SongImageUri;
        }


    }

My artist Activity :

public class ArtistActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private ValueEventListener eventListener;
    private DatabaseReference mDataRef, artist_ref;

    private List<PanelList> mList;
    private PanelAdapter panelAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.artist1layout);

        mRecyclerView = findViewById(R.id.artist_song_recyclerview);
        mDataRef = FirebaseDatabase.getInstance().getReference("ArtistView").child(getIntent().getStringExtra("Artist"))
                .child("Songs");


        mRecyclerView.setHasFixedSize(true);

        mDataRef.keepSynced(true);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mList = new ArrayList<>();

        panelAdapter = new PanelAdapter(ArtistActivity.this, mList);

        mRecyclerView.setAdapter(panelAdapter);

        eventListener = mDataRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mList.clear();
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    PanelList list = postSnapshot.getValue(PanelList.class);
                    mList.add(list);


                }

                panelAdapter.notifyDataSetChanged();
            }

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

            }
        });
    }
}

There is nothing in my FavoriteActivity except onCreate .

Don't pass the list in adapter if it is dynamic.

Add this function in your adapter

public void setPanelList(List<PanelList> panelList){

    panelList = mList;
    notifyDataSetChanged();
}

Modify your mDataRef.addValueEventListener

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

       mList.clear();

       for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

           PanelList list = postSnapshot.getValue(PanelList.class);
           mList.add(list);
       }

       panelAdapter.setPanelList(mList);
  }

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM