title: Open fragment from adapter RecyclerView
body: <p>i have some troubles with my app. I have recyclerView with CardView. And i want to open new fragment with description of info... But it doesn't matter. </p>
  
  <p>This is my adapter</p>
  
  <pre><code>public class RVAdapter extends RecyclerView.Adapter&lt;RVAdapter.DataViewHolder&gt;{
  
  //here
  private OnRecyclerViewItemClickListener mClickListener;
  
  public static class DataViewHolder extends RecyclerView.ViewHolder{
      CardView cardView;
      TextView textViewName;
      TextView textViewRegion;
  
      public DataViewHolder(View itemView) {
          super(itemView);
          cardView = (CardView) itemView.findViewById(R.id.cardView);
          textViewName = (TextView) itemView.findViewById(R.id.textViewName);
          textViewRegion = (TextView) itemView.findViewById(R.id.textViewRegion);
      }
  }
  
  private List&lt;DataJSON&gt; dataList;
  
  RVAdapter(List&lt;DataJSON&gt; dataList){
      this.dataList = dataList;
  }
  
  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
      super.onAttachedToRecyclerView(recyclerView);
  }
  
  @Override
  public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
      DataViewHolder dvh = new DataViewHolder(v);
      return dvh;
  }
  
  private DataJSON getItem(int position){
      return dataList.get(position);
  }
  
  @Override
  public void onBindViewHolder(DataViewHolder holder, final int position) {
      holder.textViewName.setText(dataList.get(position).getPierName());
      holder.textViewRegion.setText(dataList.get(position).getRegion());
  
      holder.cardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Log.d("myLog", "Click-clack position " + position);
              if (mClickListener != null){
                  Log.d("myLog", "mClickListener != null");
                  mClickListener.onClick(position);
              } else {Log.d("myLog", "mClickListener = null");}
          }
      });
  }
  
  //here
  public interface OnRecyclerViewItemClickListener {
      void onClick(int parameter);
  }
  
  @Override
  public int getItemCount() {
      return dataList.size();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In onBindViewHolder i have onClick where i call OnRecyclerViewItemClickListener onClick in MainActivity.</p>
  
  <p>This is MainActivity.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements RVAdapter.OnRecyclerViewItemClickListener{
  private CardViewFragment cardViewFragment;
  private DetailFragment detailFragment;
  
  private FragmentManager fragmentManager;
  private FragmentTransaction fragmentTransaction;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d("MyTag", "onCreate");
  
      //Create FragmentManager
      fragmentManager = getSupportFragmentManager();
  
      //Initialization fragments
      cardViewFragment = new CardViewFragment();
      detailFragment = new DetailFragment();
  
  
      fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.replace(R.id.container, cardViewFragment, CardViewFragment.TAG);
      fragmentTransaction.commit();
  }
  
  @Override
  public void onClick(int parameter) {
      Log.d("myLog", "Hi, from MainActivity");
      fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.replace(R.id.container, detailFragment, DetailFragment.TAG);
      fragmentTransaction.commit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have an error, that mClickListener is null object, and understand it,
  but i really don't know, how to fix it.</p>
  
  <p>This is CardViewFragment</p>
  
  <pre><code>public class CardViewFragment extends Fragment implements RVAdapter.OnRecyclerViewItemClickListener {
  
  public static final String TAG = "CardViewFragment";
  
  private List&lt;DataJSON&gt; dataJSONList;
  private RecyclerView recyclerView;
  RVAdapter rvAdapter;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
      View rootView = inflater.inflate(R.layout.activity_card_view_fragment, container, false);
  
      dataJSONList = new ArrayList&lt;&gt;();
      recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
  
      LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
      recyclerView.setLayoutManager(linearLayoutManager);
      recyclerView.setHasFixedSize(true);
  
      loadDataFromServer();
      //testData();
  
      rvAdapter = new RVAdapter(this, dataJSONList);
      recyclerView.setAdapter(rvAdapter);
  
      getActivity();
      return rootView;
  }
  
  private void testData(){
      //dataJSONList.add(new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia"));
      //dataJSONList.add(new DataJSON(1, "kek", "Lol", "Scream"));
      DataJSON data2 = new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia");
      dataJSONList.add(data2);
  }
  
  String strJson;
  private void loadDataFromServer()  {
  
      AsyncTask&lt;Void, Void, String&gt; task = new AsyncTask&lt;Void, Void, String&gt;(){
          @Override
          protected String doInBackground(Void... voids) {
              OkHttpClient client = new OkHttpClient();
              Request request = new Request.Builder()
                      .url("https://gesmetrics.ru/testpiers.php").build();
              Response response;
  
              try {
                  response = client.newCall(request).execute();
                  strJson = response.body().string();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              return strJson;
          }
  
          @Override
          protected void onPostExecute(String strJson) {
              super.onPostExecute(strJson);
              Log.d("myLog", strJson);
  
              //DataJSON data2 = new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia");
              //dataJSONList.add(data2);
  
              try {
                  JSONArray jsonArray = new JSONArray(strJson);
                  for (int i = 0; i &lt; jsonArray.length(); i++) {
                      JSONObject object = jsonArray.getJSONObject(i);
  
                      int id = object.getInt("pierId");
                      String name = object.getString("pierName");
                      String region = object.getString("region");
                      String description = object.getString("description");
                      DataJSON data = new DataJSON(i, name, region, description);
                      //nothing happens here
                      dataJSONList.add(data);
                  }
              } catch (JSONException e) {
                  System.out.println("Issue");
              }
              rvAdapter.notifyDataSetChanged();
          }
  
      };
      task.execute();
  }
  
  @Override
  public void onClick(int parameter) {
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>MainActivity</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements RVAdapter.OnRecyclerViewItemClickListener{
  private CardViewFragment cardViewFragment;
  private DetailFragment detailFragment;
  
  private FragmentManager fragmentManager;
  private FragmentTransaction fragmentTransaction;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d("MyTag", "onCreate");
  
      //Create FragmentManager
      fragmentManager = getSupportFragmentManager();
  
      //Initialization fragments
      cardViewFragment = new CardViewFragment();
      detailFragment = new DetailFragment();
  
  
      fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.replace(R.id.container, cardViewFragment, CardViewFragment.TAG);
      fragmentTransaction.commit();
  }
  
  @Override
  public void onClick(int parameter) {
      Log.d("myLog", "Hi, from MainActivity");
      fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.replace(R.id.container, detailFragment, DetailFragment.TAG);
      fragmentTransaction.commit();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Thanks a lot!</p>