title: How to pass data from a activity to a recycler view adapter in android
body: <p>I'm trying to design a page where address are stored in recycler view -&gt; cardview.</p>
  <p>When the user clicks the <strong>add address button</strong> from the <strong>Activity A</strong> the user is navigated to the add address page in <strong>Activity B</strong>. Here the user can input customer name, address line 1 and address line two.</p>
  <p>And once save button is clicked in <strong>Activity B</strong>, a cardview should be created under the <strong>add address button</strong> in the <strong>Activity A</strong>.</p>
  <p>This design is just like the amazon mobile app add address option.</p>
  <p>Could anyone give me an example hoe to pass the saved data from activity to recycler adapter. I know how to pass data from recycler adapter to activity with putExtra etc..</p>
  <p>Kindly help me. Million Thanks in advance!</p>
  <p><strong>Code In Activity A(Where the Add address button is available and where the recycler view is present)</strong></p>
  <pre><code>public class ProfileManageAdressFragment extends AppCompatActivity {
  
      RecyclerView recyclerView;
      ProfileManageAddressRecyclerAdapter adapter;
      ArrayList&lt;ProfileManageAddressGetterSetter&gt; reviews;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_profile_manage_adress);
  
          Button addAddress = findViewById(R.id.addNewAddress);
          reviews = new ArrayList&lt;&gt;();
          addAddress.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(v.getContext(), &quot;Clicked&quot;, Toast.LENGTH_SHORT).show();
                  Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
                  startActivity(intent);
                  
              }
          });
      }
  }
  
  </code></pre>
  <p>Piece of Code that is responsible for adding a card view in <strong>Activity A</strong>. Kindly let me know how to invoke this below code on button click in Activity</p>
  <pre><code>reviews.add(new ProfileManageAddressGetterSetter(&quot;Customer Name&quot;, &quot;address line 1&quot;, &quot;address line 2&quot;));
                  recyclerView = findViewById(R.id.addressRecyclerView);
                  recyclerView.setHasFixedSize(true);
                  recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
                  adapter = new ProfileManageAddressRecyclerAdapter(this, reviews);
                  recyclerView.setAdapter(adapter);
  </code></pre>
  <p><strong>Code in the Recycler adapter</strong></p>
  <pre><code>public class ProfileManageAddressRecyclerAdapter extends RecyclerView.Adapter&lt;ProfileManageAddressRecyclerAdapter.ViewHolder&gt; {
  
      private ArrayList&lt;ProfileManageAddressGetterSetter&gt; mDataset = new ArrayList&lt;&gt;();
      private Context context;
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          private TextView customer_name, address_one, address_two;
          private Button edit, remove;
  
          public ViewHolder(View v) {
              super(v);
              customer_name = (TextView) v.findViewById(R.id.customerName);
              address_one = (TextView) v.findViewById(R.id.addressLineOne);
              address_two = v.findViewById(R.id.addressLineTwo);
              
          }
      }
  
      public ProfileManageAddressRecyclerAdapter(View.OnClickListener profileManageAdressFragment, ArrayList&lt;ProfileManageAddressGetterSetter&gt; dataset) {
          mDataset.clear();
          mDataset.addAll(dataset);
      }
  
      @Override
      public ProfileManageAddressRecyclerAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
  
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_manage_address, parent, false);
          ProfileManageAddressRecyclerAdapter.ViewHolder vh = new ProfileManageAddressRecyclerAdapter.ViewHolder(view);
          return vh;
      }
  
      @Override
      public void onBindViewHolder(@NonNull ProfileManageAddressRecyclerAdapter.ViewHolder holder, int position) {
          ProfileManageAddressGetterSetter profileManageAddressGetterSetter = mDataset.get(position);
          holder.address_one.setText(profileManageAddressGetterSetter.getAddress_line_1());
          holder.address_two.setText(profileManageAddressGetterSetter.getGetAddress_line_2());
          holder.customer_name.setText(profileManageAddressGetterSetter.getContractor_name());
      }
  
  
      @Override
      public int getItemCount() {
          return mDataset.size();
      }
  
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/m4x7S.png" rel="nofollow noreferrer">enter image description here</a> - After trying the call from adapter using intent as mentioned above ended up with a 0.</p>
  <p>Code in the Activity B</p>
  <pre><code>public class AddNewAddress extends AppCompatActivity {
  
      private EditText customer_name, address_one, address_two;
      private TextView cancel;
      private Button add_address;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add_new_address);
  
          customer_name = findViewById(R.id.customerName);
          address_one = findViewById(R.id.addressOne);
          address_two = findViewById(R.id.addressTwo);
          add_address = findViewById(R.id.addAddress);
          cancel = findViewById(R.id.completeCancel);
  
          String cancel_text = &quot;Cancel&quot;;
  
          SpannableString spanableObject = new SpannableString(cancel_text);
          ClickableSpan clickableSpan = new ClickableSpan() {
              @Override
              public void onClick(View widget) {
                  Toast.makeText(AddNewAddress.this, &quot;Clicked&quot;, Toast.LENGTH_SHORT).show();
              }
  
              @Override
              public void updateDrawState(TextPaint ds) {
                  super.updateDrawState(ds);
                  ds.setColor(Color.BLUE);
              }
          };
  
          spanableObject.setSpan(clickableSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          cancel.setText(spanableObject);
  
          cancel.setMovementMethod(LinkMovementMethod.getInstance());
  
          final ProfileManageAdressFragment profileManageAdressFragment = new ProfileManageAdressFragment();
          add_address.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(AddNewAddress.this, ProfileManageAdressFragment.class);
                  startActivity(intent);
              }
          });
      }
  
      private void setFragment(android.support.v4.app.Fragment fragment) {
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.main_frame, fragment).commit();
      }
  }
  
  </code></pre>
  <p><strong>Update 1:</strong></p>
  <p>Kindly check my updated Recycler adapter. When I run this 0 is displayed in the text area as shown in the attached image. I'm new to android. Kindly help with example.</p>