title: How can i call recyclerview adapter in another class?
body: <p>I wanna update my <code>RecyclerView</code> list in another class. I can't call <code>RecyclerView</code> adapter in the second class, actually when the adapter is initialized there, an error is shown "classXXX is not an enclosing class". How can I do that? </p>
  
  <p>Here is my <code>Fragment</code> which contains <code>RecyclerView</code> named <code>AllworksFragment</code>:</p>
  
  <pre><code>package ir.applinkfinder.hw6;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.design.widget.FloatingActionButton;
  import android.support.v4.app.Fragment;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import java.util.List;
  import ir.applinkfinder.hw6.model.WorksModel;
  import ir.applinkfinder.hw6.model.WorksRepository;
  
  /**
  * A simple {@link Fragment} subclass.
   */
  public class AllworksFragment extends Fragment {
  
  private RecyclerView mRecyclerView;
  private MyAdapter myAdapter;
  private Button mButtonInsert;
  private Button mButtonRemove;
  private EditText mEditTextInsert;
  private EditText mEditTextRemove;
  private FloatingActionButton mFloatingActionButtonAdd;
  private Button mButtonAdd;
  
  public AllworksFragment() {
      // Required empty public constructor
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_allworks, container, false);
  
      mRecyclerView = view.findViewById(R.id.recyclerview);
      mFloatingActionButtonAdd = view.findViewById(R.id.floatingActionButton_add);
      mButtonAdd = view.findViewById(R.id.button_test_addd);
  
      mButtonAdd.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent = AddItemToListActivity.newIntent(getActivity());
              startActivity(intent);
          }
      });
  
      mFloatingActionButtonAdd.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  //                Intent intent = AddItemToListActivity.;
                  Intent intent = AddItemToListActivity.newIntent(getActivity());
              startActivity(intent);
          }
      });
  
      myRecyclerViewLayoutManager();
      updateUI();
  
      return view;
  }
  
  private void myRecyclerViewLayoutManager(){
          mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); // moshakhas mikone recyclerview chejuri namayesh dade beshe
  //        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
  
     //        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
  //        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL) ;
   //        mRecyclerView.setLayoutManager(linearLayoutManager);
  }//myRecyclerViewLayoutManager();
  
  private void updateUI(){
      WorksRepository worksRepository = WorksRepository.getInstance();
      List&lt;WorksModel&gt; works = worksRepository.getmWorksList();
      if (myAdapter == null) {
          myAdapter = new MyAdapter(works);
          mRecyclerView.setAdapter(myAdapter);
      }
      else{
  //            workAdapter.setCrimeList(works);
  //            myAdapter.setWorkList(works);
  
      /*
      notifyDataSetChanged() method should be called after
      you've updated the list which holds all your data,
      in order to refresh the RecyclerView
      */
  
          myAdapter.notifyDataSetChanged(); // jahate update e list bad az taqyir e edit text ha, in kole list racheck mikonad
  
       //            myAdapter.notifyItemChanged(positionnnn); // notifyItemChanged faqat haman position e khas ra check mikonad
      }
  }//updateUI
  
  
  //ViewHolder
  public class myViewHolder extends RecyclerView.ViewHolder{
  
      protected TextView mTextViewTitle;
      protected TextView mTextViewDetail;
      protected TextView mTextViewDate;
      protected TextView mTextViewHour;
  
      public myViewHolder(View itemView) {
          super(itemView);
          mTextViewTitle  =  itemView.findViewById(R.id.textview_title);
          mTextViewDetail =  itemView.findViewById(R.id.textview_detail);
          mTextViewDate   =  itemView.findViewById(R.id.textview_date);
          mTextViewHour   =  itemView.findViewById(R.id.textview_hour);
  
  
      }
  }//ViewHolder
  
   //    ///////////////////////////////////////////////////////////
   //    CustomListListener mListener;
   //    public interface CustomListListener{
   //        void onListChanged(List&lt;WorksModel&gt; myList);
   //    }
   //    public void setOnListChangeListener(CustomListListener listener){
   //        this.mListener = listener;
   //    }
  
  
  
      // Adapter
  public class MyAdapter extends RecyclerView.Adapter&lt;myViewHolder&gt;{
  
      private List&lt;WorksModel&gt; worksList;
  
      public MyAdapter(List&lt;WorksModel&gt; workM) {
          worksList = workM;
      }
  
      @Override
      public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_list, parent, false);
          return new myViewHolder(itemView);
      }
  
      @Override
      public void onBindViewHolder(myViewHolder holder, final int position) {
          WorksModel worksModel = worksList.get(position);
          holder.mTextViewTitle.setText(worksModel.getTitle());
          holder.mTextViewDetail.setText(worksModel.getDetail());
   //            holder.mTextViewDate.setText(worksModel.getDate());
          holder.mTextViewHour.setText(worksModel.getHour());
  
  
  
          holder.itemView.setOnClickListener(new View.OnClickListener() { 
              @Override
              public void onClick(View v) {
  
                  Intent intent = WorkDetailActivity.newIntent(getActivity(), worksList.get(position).getTitle());
                  startActivity(intent);
              }
          });
  
    }
  
  //        private void myAddItemMethod(int position, WorksRepository worksRepository){
  
     //        }
  
      @Override
      public int getItemCount() {
          return worksList.size();
         }
      }//Adapter
  
   }
  </code></pre>
  
  <p>And this is the <code>Fragment</code> which i wish to add some new data to <code>RecyclerView</code>:</p>
  
  <pre><code> package ir.applinkfinder.hw6;
  import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.support.v7.widget.RecyclerView;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.EditText;
   import android.widget.ImageButton;
  
   import java.util.ArrayList;
   import java.util.Date;
  
   import ir.applinkfinder.hw6.model.WorksModel;
   import ir.applinkfinder.hw6.model.WorksRepository;
  
   public class AddItemToListFragment extends Fragment{ // implements WorksRepository.CustomListListener {
  
      WorksRepository mWorksRepository;
  
       private ArrayList&lt;WorksModel&gt; mWorksList;
  
       private RecyclerView.Adapter mAdapter;
  
  
       private EditText mEditTextAddTitle;
       private EditText mEditTextAddDetail;
       private EditText mEditTextAddDate;
       private EditText mEditTextAddHour;
       private ImageButton mImageButtonAddDone;
  
  public AddItemToListFragment() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_add_item_to_list, container, false);
  
   //        WorksRepository.setOnListChangeListener(this);
  
      mEditTextAddTitle   = view.findViewById(R.id.edittext_add_title);
      mEditTextAddDetail  = view.findViewById(R.id.edittext_add_details);
      mEditTextAddDate    = view.findViewById(R.id.edittext_add_date);
      mEditTextAddHour    = view.findViewById(R.id.edittext_add_hour);
      mImageButtonAddDone = view.findViewById(R.id.imagebutton_add_done);
  
      mImageButtonAddDone.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              insertItem();
          }
      });
  
      return view;
  }
  
  public void insertItem(){
      mAdapter = new AllworksFragment.MyAdapter(mWorksRepository.getmWorksList());
   //        mAdapter = new AllworksFragment.MyAdapter(mWorksRepository);
  
      mWorksRepository = new WorksRepository();
      WorksModel worksModel = new WorksModel();
      worksModel.setTitle(mEditTextAddTitle.getText().toString());
      worksModel.setDetail(mEditTextAddDetail.getText().toString());
   //        worksModel.setDate(Date);
      worksModel.setDate(new Date());
      worksModel.setHour(mEditTextAddHour.getText().toString());
  
      mWorksRepository.getmWorksList().add(worksModel);
   //        mWorksList.add(worksModel);
  
      mAdapter.notifyDataSetChanged();
  }//insertItem
  }
  </code></pre>
  
  <p>The problem is that in <code>insertItem()</code> method, I can't initialize <code>RecyclerView</code> adapter. It invokes error:
  "<code>AllworksFragment</code> is not an enclosing fragment"</p>