title: Pass JSON Array in RecyclerView Adapter Class to Activity Class
body: <p>I have a RecyclerView and am creating a JSON Array with the ids of elements which are clicked in the onClick method. I am able to generate the JSON Array but I am facing trouble passing this JSON Array to my Activity. </p>
  
  <p>I created an interface :</p>
  
  <pre><code>public interface ClientDataTransfer {
  public void getValues(JSONArray jsonArray);
  }
  </code></pre>
  
  <p>and modified the Adapter constructor like this:</p>
  
  <pre><code>public ClientListAdapter(List&lt;ClientListData&gt; clientListData, Context context, ClientDataTransfer clientDataTransfer) {
      super();
  
      this.clientListData = clientListData;
      this.context = context;
      this.clientDataTransfer = clientDataTransfer;
  }
  </code></pre>
  
  <p>and in my Activity class I am setting the adapter like this:</p>
  
  <pre><code>recyclerViewAdapter = new ClientListAdapter(clientListData, this, this);
              recyclerView.setAdapter(recyclerViewAdapter);
  </code></pre>
  
  <p>I implemented the Interface in the Activity class and inherited the method  but I am facing trouble getting the JSON Array in the Activity class.</p>
  
  <p>I am creating the JSON Array inside the setOnClickListener method in onBindViewHolder.</p>
  
  <p>Here is my Adapter Class:</p>
  
  <pre><code>public class ClientListAdapter extends RecyclerView.Adapter&lt;ClientListAdapter.ViewHolder&gt; {
  
  private Context context;
  private List&lt;ClientListData&gt; clientListData;
  public JSONArray clientArray = new JSONArray();
  public ClientDataTransfer clientDataTransfer;
  
  
  public ClientListAdapter(List&lt;ClientListData&gt; clientListData, Context context, ClientDataTransfer clientDataTransfer) {
      super();
  
      this.clientListData = clientListData;
      this.context = context;
      this.clientDataTransfer = clientDataTransfer;
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.client_list_item, parent, false);
      ViewHolder viewHolder = new ViewHolder(view);
  
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(final ClientListAdapter.ViewHolder holder, final int position) {
  
      final ClientListData clientListDataModel = clientListData.get(position);
      holder.clientList.setText(clientListDataModel.getClientName());
  
      holder.itemView.setBackgroundColor(clientListDataModel.isSelected() ? Color.GRAY : Color.WHITE);
      holder.clientList.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              clientListDataModel.setSelected(!clientListDataModel.isSelected());
  
              try {
                  JSONObject clientObject = new JSONObject();
                  if(clientListDataModel.isSelected()) {
                      holder.itemView.setBackgroundColor(Color.GRAY);
                      clientObject.put("id", clientListData.get(position).getClientId());
                      clientArray.put(clientObject);
                  } else if(!clientListDataModel.isSelected()) {
  
                      holder.itemView.setBackgroundColor(Color.WHITE);
  
                      for (int i = 0; i &lt; clientArray.length(); i++) {
                          clientObject = clientArray.getJSONObject(i);
                          if (clientObject.getString("id").equals(clientListDataModel.getClientId())) {
                              clientArray=removeFromJsonArray(clientArray,i);
                              break;
                          }
                      }
                  }
                  //clientArray.put(clientObject);
  
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              clientDataTransfer.getValues(clientArray);
              Log.e("client id array", ""+clientArray);
          }
      });
  }
  
  @Override
  public int getItemCount() {
      return clientListData == null ? 0:clientListData.size();
  }
  
  class ViewHolder extends RecyclerView.ViewHolder {
  
      public TextView clientList;
  
      public ViewHolder(View itemView) {
          super(itemView);
  
          clientList = (TextView) itemView.findViewById(R.id.tv_client_list);
  
      }
  }
  
  private JSONArray removeFromJsonArray(JSONArray array,int position) {
      JSONArray newArray = new JSONArray();
      for (int i=0;i&lt;array.length();i++)
      {
          //Excluding the item at position
          if (i != position)
          {
              try {
                  newArray.put(array.get(i));
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      }
      return newArray;
  }
  }
  
  holder.clientList.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              clientListDataModel.setSelected(!clientListDataModel.isSelected());
  
              try {
                  JSONObject clientObject = new JSONObject();
                  if(clientListDataModel.isSelected()) {
                      holder.itemView.setBackgroundColor(Color.GRAY);
                      clientObject.put("id", clientListData.get(position).getClientId());
                      clientArray.put(clientObject);
                  } else if(!clientListDataModel.isSelected()) {
  
                      holder.itemView.setBackgroundColor(Color.WHITE);
  
                      for (int i = 0; i &lt; clientArray.length(); i++) {
                          clientObject = clientArray.getJSONObject(i);
                          if (clientObject.getString("id").equals(clientListDataModel.getClientId())) {
                              clientArray=removeFromJsonArray(clientArray,i);
                              break;
                          }
                      }
                  }
                  //clientArray.put(clientObject);
  
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              clientDataTransfer.getValues(clientArray);
              Log.e("client id array", ""+clientArray);
          }
      }); 
  </code></pre>
  
  <p>Here is my Activity Class where I am implementing the Adapter:</p>
  
  <pre><code>public class ClientListActivity extends AppCompatActivity implements View.OnClickListener, ClientDataTransfer {
  
  private String groupId;
  private String clientId;
  private String clientName;
  private RecyclerView recyclerView;
  private List&lt;ClientListData&gt; clientListData;
  private RecyclerView.LayoutManager recyclerViewLayoutManager;
  private RecyclerView.Adapter recyclerViewAdapter;
  private JSONArray clientArrayList = new JSONArray();
  ClientDataTransfer clientDataTransferx;
  
  private Toolbar toolbar;
  private FloatingActionButton share;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_client_list);
  
      recyclerView = (RecyclerView) findViewById(R.id.rv_client_list);
      toolbar = (Toolbar) findViewById(R.id.client_list_toolbar);
      share = (FloatingActionButton) findViewById(R.id.floating_action_button);
      share.setOnClickListener(this);
  
      if (android.os.Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
          Window window = getWindow();
          window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
          window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorSecondary));
      }
  
      setSupportActionBar(toolbar);
      ActionBar actionBar = getSupportActionBar();
      actionBar.setDisplayHomeAsUpEnabled(true);
  
      clientListData = new ArrayList&lt;&gt;();
      recyclerViewLayoutManager = new LinearLayoutManager(this);
      recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
      recyclerView.setItemAnimator(new DefaultItemAnimator());
      recyclerView.setLayoutManager(recyclerViewLayoutManager);
  
      if(getIntent() != null) {
  
          Bundle bundle = getIntent().getExtras();
          try {
              JSONObject jsonClientListObject = new JSONObject(getIntent().getStringExtra("clientList"));
              JSONArray clientArray = jsonClientListObject.optJSONArray("activeClients");
  
              for(int i = 0 ; i &lt; clientArray.length(); i++) {
                  ClientListData clientListData = new ClientListData();
                  clientListData.setClientId(clientArray.getJSONObject(i).getString("id"));
                  clientListData.setClientName(clientArray.getJSONObject(i).getString("name"));
  
                  clientId = clientListData.getClientId();
                  clientName = clientListData.getClientName();
              }
              groupId = bundle.getString("groupId");
  
              for(int i=0; i&lt;clientArray.length(); i++) {
                  ClientListData clientListDataModel = new ClientListData();
                  JSONObject jsonObject = null;
                  jsonObject = clientArray.getJSONObject(i);
                  clientListDataModel.setClientName(jsonObject.getString("name"));
                  clientListDataModel.setClientId(jsonObject.getString("id"));
                  clientListData.add(clientListDataModel);
              }
  
              recyclerViewAdapter = new ClientListAdapter(clientListData, this, this);
              recyclerView.setAdapter(recyclerViewAdapter);
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }
  
  /*@Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      getMenuInflater().inflate(R.menu.select_all, menu);
  
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
      switch (item.getItemId()) {
  
          case R.id.select_all:
  
              break;
      }
  
      return super.onOptionsItemSelected(item);
  }*/
  
  @Override
  public void getValues(JSONArray jsonArray) {
  
  }
  
  @Override
  public void onClick(View v) {
      switch (v.getId()) {
  
          case R.id.floating_action_button:
              clientDataTransferx = new ClientDataTransfer() {
                  @Override
                  public void getValues(JSONArray jsonArray) {
                      clientDataTransferx.getValues(clientArrayList);
                  }
              };
              break;
      }
  }
  
  }
  </code></pre>