I have an activity with a recycler view adapter class when a button is clicked in the adapter class. I want to update the main activity

I have an activity with a RecyclerView.Adapter class. The RecyclerView contains a button. When this button is clicked, I want to change the value defined in the main activity and display it in a TextView that is also defined in the main activity, otherwise I want to update the activity.

I've already tried some code like:

+3


source to share


3 answers


In this case, you don't need to resume activity. The best solution would be:

  • Declare interface.
  • Implement it in Activity (your updates to TextView etc. are implemented here).
  • pass an object that implements the interface for your adapter.
  • Call the method on this interface in the adapter.


Here's an example: fooobar.com/questions/713174 / ...

0


source


YourActivity extends Activity implements YourAdapter.ClickCallback{

@override
oncreate(Bundle bundle){
 // initialize views 
 YourAdapter adapter = new YourAdapter(this);
 // do something with your adapter
}

@Override
public void updateView(){
//update your views here
}   

}

//In Recycler Adapter pass ClickCallBack As parameter
class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

 private ClickCallBack clickCallBack;

 public YourAdapter(ClickCallBack clickCallBack){
 this.clickCallBack=clickCallBack;
 }

 //ClickCallback Interface
interface ClickCallBack {
 void updateView();
}

 //In your ButtonClick
  clickCallBack.updateView();

}

      



You can also add parameters to the updateView method.

0


source


Pass an instance of this TextView constructor through the constructor and update it there in the Adapter. This would be the easiest way.

0


source







All Articles