3

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

I already tried some code like:

Andrii Lisun
  • 653
  • 4
  • 22

3 Answers3

0

You don't need to recreate Activity in this case. The best solution would be:

  1. Declare an interface.
  2. Implement it in Activity(here implement your updates to TextView, etc.).
  3. Pass object that implements the interface to your adapter.
  4. Call the method on this interface in adapter.

Here is example: https://stackoverflow.com/a/32720879/2528967

Andrii Lisun
  • 653
  • 4
  • 22
0
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 in updateView method.

Santhosh
  • 160
  • 7
0

Pass an instance of those TextView through constructor and update the value there in the Adaptor. That would be the easiest way.

Ashutosh Sagar
  • 981
  • 8
  • 20