0

enter image description here

When I click the PLAY button in Activity, I want to get some data in RECYCLERVIEW (For example, Song1, Song2, Song3.. data). How can I do that? Can you help with the code? I'm new at this. Here are my codes:

This is Model:

public class MyModel {

String songName, songURL;

public MyModel(String _songName, String _songURL) {
    this.songName = _songName;
    this.songURL = _songURL;
}

// Getters and setters

}

This is a Adapter:

public class MyAdaptor extends RecyclerView.Adapter<MyAdaptor.MyHolder> {

    ArrayList<MyModel> ayetList;
    LayoutInflater layoutInflater;
    Context mContext1;
    View view;

    public MyAdaptor(Context mContext, ArrayList<MyModel> _ayetList) {
        this.ayetList = _ayetList;
        mContext1 = mContext;
        layoutInflater = LayoutInflater.from(mContext);
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        TextView tv_songname;
        SeekBar sb_song;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_songname = itemView.findViewById(R.id.tv_songname);
            sb_song = itemView.findViewById(R.id.sb_song);
        }

        public void setData(MyModel ayetModel, int pozisyon) {
            tv_songname.setText(ayetModel.getSongName());
           
        }

    }

    @NonNull
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = layoutInflater.inflate(R.layout.song_item, parent, false);
        return new MyHolder(view);
    }

    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        MyModel selectedProduct = ayetList.get(position);
        holder.setData(selectedProduct, position);
    }

    public int getItemCount() {
        return ayetList.size();
    }

}

This is the part I want to process. Here I want to get the data from the recyclerview by clicking the PLAY button.

This is a Activity:

Button btnPlay;
RecyclerView rwSongs;
Context context = this;
MyAdaptor myAdaptor;
MyModel myModel;
ArrayList<MyModel> mainList = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    btnPlay = findViewById(R.id.btnPlay);
    rwSongs = findViewById(R.id.rwSongs);
    
    rwSongs.setLayoutManager(new LinearLayoutManager(context));
    
    getData();

   img_Play.setOnClickListener(view -> {
       //I don't know what to do next ...
   });


}


private void getData() {
    .
    .
    .
    for (int i = 0; i < _dataArrayItiraf.length(); i++) {
        JSONObject jsonItiraf = _dataArrayItiraf.getJSONObject(i);
        String _SongName = jsonItiraf.getString("SongName");
        String _SongURL = jsonItiraf.getString("SongURL");

        myModel = new MyModel(_SongName, _SongURL);
        myModel.setSongName(_SongName);
        myModel.setSongURL(_SongURL);
        mainList.add(myModel);
    }
    myAdaptor = new MyAdaptor(context, mainList);
    rwSongs.setAdapter(myAdaptor);
    .
    .
    .
}

I will look forward to all your help. Thanks...

  • do you want like I click on the first song. Then I click play, so the first song start playing? – Sambhav Khandelwal Mar 20 '22 at 09:46
  • Please explain exactly what you wish to do with the data from the RecyclerView.. Not "for example".. – gioravered Mar 20 '22 at 09:46
  • @SambhavKhandelwal No. I will never click on Recyclerview. This is what I want to do: I will click the Play button in the Activity and get the names of the items (Song1, Song2, Song3 etc.) in the recyclerview. – Fatih Demir Mar 20 '22 at 10:27
  • you want to get the name of the song? – Sambhav Khandelwal Mar 20 '22 at 10:29
  • @gioravered I only need the names of the items in the Recyclerview (such as Song1, Song2, Song3...) as Strings. I need to get it by clicking the button in Activity. I shouldn't interfere with Recyclerview. I can't speak English well. Can you help me? – Fatih Demir Mar 20 '22 at 10:30
  • @SambhavKhandelwal Yes.. But as I said, I want to perform this operation without clicking Recyclerview. I will do this by clicking the play button in the Activity. – Fatih Demir Mar 20 '22 at 10:32
  • @FatihDemir But you already have the data of the RecyclerView in this variable: mainList. Just make it a class member and access it when you need to. – gioravered Mar 20 '22 at 10:38
  • @gioravered can you show me a sample code? I'm new at this job.. – Fatih Demir Mar 20 '22 at 10:42
  • for (MyModel model: mainList) { Log.i("LOG", model.songName); } – gioravered Mar 20 '22 at 10:58
  • @gioravered I want to ask one more thing. I want to change the background color of any item in the Recyclerview by clicking the button in the Activity. For example, I pressed the play button, I want to change item 3 (Song3 item) in Recyclerview to red color. I want to change the background color of the item. How can I do that? – Fatih Demir Mar 20 '22 at 11:15
  • This question was probably answered before. A quick search came up with this: https://stackoverflow.com/questions/39146035/change-recycler-view-item-background-color-by-code – gioravered Mar 20 '22 at 11:41
  • @gioravered You misunderstand me :) I don't want to interfere with the recyclerview. I want to perform the operation I mentioned by pressing the PLAY button in the Activity. – Fatih Demir Mar 20 '22 at 11:53
  • You do want to change the color of the items in the recycler view. Please open another question for this issue in case you can't the answer on StackOverflow. – gioravered Mar 20 '22 at 12:14
  • @gioravered Yes, I do want to change the color of the items in the recycler view. But I want to perform this operation by pressing the PLAY button in the Activity. It's probably very simple, but I don't know. – Fatih Demir Mar 20 '22 at 12:45

0 Answers0