0

In my RecyclerViewAdapter, part of my codes are below:

@Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item_lists, parent, false);
        return new ViewHolder(v);
    }
    class ViewHolder extends RecyclerView.ViewHolder {
       
        EqualizerView equalizerView, equalizerr;
        CardView image_card;

        ViewHolder(View itemView) {
            super(itemView);
            
            equalizerView = itemView.findViewById(R.id.mini_eq_view);
            image_card = itemView.findViewById(R.id.main_card_view_play);
            equalizerView.stopBars();
            equalizerView.setVisibility(View.GONE);
        }
    }

Then in MainActivity, part of the codes are below

@Override
    public void playSongComplete() {
        Log.d("complete", "Playing song is complete: ");

    }

What I want to do is, if playing song is complete, I want to call

equalizerView.stopBars();
equalizerView.setVisibility(View.GONE);

from inside method playingSongComplete(). Is there any way to do that?

King Maf
  • 42
  • 1
  • 9
  • do you have any list of data in your adapter which can be accessed by the viewholder? – dito cesartista Apr 12 '22 at 09:34
  • `private ArrayList arrayList;` is accessed inside `ViewHolder` and `onBindViewHolder`. And `private Activity activity;` accessed inside `onBindViewHolder`. – King Maf Apr 12 '22 at 15:59

2 Answers2

0

create a function inside your adapter like

void stopEqualiser(){
   equalizerView.stopBars();
   equalizerView.setVisibility(View.GONE);
}

then in the activity when music complete

@Override
public void playSongComplete() {
    
  // your recycler adapter
    adapter.stopEqualiser();

}
Flying Dutchman
  • 365
  • 6
  • 13
0

As far as I know, we cannot access variables inside viewholder from an activity. What we can do however is modify the data of the adapter and notify the adapter about the changed data. We can then handle the data change inside the onBindViewHolder method

you can create a boolean variable called isSongComplete with initial value false inside the adapter, then create function inside your adapter like this

void setComplete(){
    isSongComplete = true;
    notifyDataSetChanged();
}

and in your onBindViewHolder method inside your viewholder class

@Override 
public void onBindViewHolder(ViewHolder holder, int position) {
    if (isSongComplete) {
        equalizerView.stopBars();
        equalizerView.setVisibility(View.GONE);   
    }
}

then in your MainActivity

@Override
public void playSongComplete() {
    Log.d("complete", "Playing song is complete: ");
    adapter.setComplete();
}
dito cesartista
  • 209
  • 1
  • 10
  • Thanks @dito cesartista. I have tried this but I got this errors "Attempt to invoke virtual method 'void com.music.player.adapters.RecyclerViewAdapter.setComplete()' on a null object reference". Any solution for this error? – King Maf Apr 12 '22 at 15:51
  • looks like `playSongComplete()` is called BEFORE `adapter` is initialized, thus [`NullPointerException`](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – snachmsm Apr 12 '22 at 18:58
  • just like @snachmsm said, if you want to modify something inside adapter or viewholder, you have to make sure that the adapter is not null before calling `playSongComplete()`. Maybe you can add more information on when `playSongComplete()` is called on your code and when the adapter is initialized. – dito cesartista Apr 13 '22 at 00:23