title: Android, Can I get list data to persist when using a recyclerview
body: <p>I am trying to use a RecyclerView to display a list of data in .MainActivity.  When I used ListView with an adapter, I could access the source data ArrayList using the position clicked in order to pass the list element to an edit activity. Using a RecyclerView, the ArrayList to populate the RecyclerView is Null when the onListItemClicked attempts to access it to get the list element and add it to the Intent.  I assume this is because the mechanism to handle the click is done outside of the MainActivity class, unlike it was with ListView, and so the variable no longer exists once onCreate is finished, but I'm not sure.</p>
  <p>I don't want to read the list data from the views (do I?).  Unless there is a way to make this list persistent the only thing I can think of is to either have the main activity read a file to build the list and then read it again to get the list element to pass to the edit activity, or to have the main activity pass the element position/index and have the edit activity access the list file to retrieve the element.  But both of these seem rather file access intensive and would require the edit activity to manage the list as well as the main activity.</p>
  <pre><code>public class MainActivity extends AppCompatActivity
          implements PlanAdapter.ListItemClickListener
  {
      // View elements
      private RecyclerView planDisplay;
  
      // Adapter for building the RecyclerView
      private PlanAdapter mListAdapter;
  
      // Layout manager for the RecyclerView
      RecyclerView.LayoutManager layoutManager;
  
      // Plan list
      private static ArrayList&lt;PlanElement&gt; thisPlan;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          planDisplay = findViewById(R.id.rv_plan_display);
  
          ArrayList&lt;PlanElement&gt; thisPlan = buildThisPlan();
  
          layoutManager =
                  new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
          planDisplay.setLayoutManager(layoutManager);
          planDisplay.setHasFixedSize(true);
  
          mListAdapter = new PlanAdapter(thisPlan, this);
          planDisplay.setAdapter(mListAdapter);
  
      }
  
      @Override
      public void onListItemClick(int clickedItemIndex)
      {
          Intent i = new Intent(MainActivity.this, EditPlanItemActivity.class);
  
          i.putExtra(&quot;elementNumber&quot;, clickedItemIndex + 1);
  
  // Where the error occurs
          i.putExtra(&quot;PlanElement&quot;, thisPlan.get(clickedItemIndex));
  
          startActivity(i);
      }
  
  }
  
  </code></pre>
  <pre><code>public class PlanAdapter extends RecyclerView.Adapter&lt;PlanAdapter.AdapterViewHolder&gt;
  {
      private ArrayList&lt;PlanElement&gt; thisPlan;
  
      final private ListItemClickListener mOnClickListener;
  
      // Click Listener Interface
      public interface ListItemClickListener
      {
          void onListItemClick(int clickedItemIndex);
      }
  
      public PlanAdapter(ArrayList&lt;PlanElement&gt; aPlan, ListItemClickListener listener)
      {
          thisPlan = aPlan;
          mOnClickListener = listener;
      }
  
      @NonNull
      @Override
      public PlanAdapter.AdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
      {
          Context context = parent.getContext();
          int layoutListItem = R.layout.list_element;
  
          LayoutInflater inflater = LayoutInflater.from(context);
  
          View view = inflater.inflate(layoutListItem, parent, false);
  
          return new AdapterViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull AdapterViewHolder holder, int position)
      {
          PlanElement thisElement = thisPlan.get(position);
  
  
          holder.textItem1Display.setText(String.format(&quot;%d&quot;, thisElement.getItem1()));
      holder.textItem2Display.setText(String.format(&quot;%d&quot;, thisElement.getItem2()));
  
      }
  
      @Override
      public int getItemCount()
      {
          return thisPlan.size();
      }
  
      public class AdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
      {
          public TextView textItem1Display;
          public TextView textItem2Display;
  
          public AdapterViewHolder(@NonNull View itemView)
          {
              super(itemView);
  
              textItem1Display = itemView.findViewById(R.id.textItem1);
              textItem2Display = itemView.findViewById(R.id.textItem2);
  
              itemView.setOnClickListener(this);
          }
  
          @Override
          public void onClick(View view)
          {
              int clickedPosition = getAdapterPosition();
              mOnClickListener.onListItemClick(clickedPosition);
          }
      }
  }
  </code></pre>
  <p>Ok, added my adapter.  But the adapter works fine.  The problem occurs when the the onClick method tries to access a null ArrayList.</p>