title: How to add child item to last item from RecyclerView?
body: <p>Actually i'm trying to make an app that take restaurant orders and i'm having some issues after adding child item to my order.</p>
  
  <p>I have a recyclerView where i have different type's of food when i press on one of them i add it to recyclerView that work's something like a notebook, then i have an button that open an AlertDialog in which there is another recyclerView with variant's.</p>
  
  <p>Example: i press on PIZZA from food recyclerView it's add it to the notebook after i press on variant's and i can add "WITH PEPERONI" or "LARGE" or both.</p>
  
  <p>I have followed <a href="https://android.jlelse.eu/get-expandable-recyclerview-in-a-simple-way-8946046b4573" rel="nofollow noreferrer">this</a> guide ( <a href="https://github.com/RohitSurwase/AndroidDesignPatterns/tree/master/dynamic-recycler-view" rel="nofollow noreferrer">here</a> github project from the guide) for making the recyclerView with child item.</p>
  
  <p>Now the issue is how can i add the child item to the last parent item?</p>
  
  <p>like i'm adding parent item using this:</p>
  
  <pre><code>                dummyParentDataItem = new ItemPTERM();
                  dummyParentDataItem.setButton_name(filteredList.get(position).getDeskS());
                  dummyParentDataItem.setQuant( Integer.parseInt(filteredList.get(position).getQuant()));
                  dummyChildDataItems = new ArrayList&lt;&gt;();
  
                  dummyParentDataItem.setChildDataItems(dummyChildDataItems);
                  itemCassas.add(dummyParentDataItem);
  </code></pre>
  
  <p>And this when i add a child from variant's recyclerView</p>
  
  <pre><code>                dummyChildDataItem = new DummyChildDataItem();
                  dummyChildDataItem.setChildName(tipo.toString());
                  dummyChildDataItems.add(dummyChildDataItem);
  
                  dummyParentDataItem.setChildDataItems(dummyChildDataItems);
                  itemCassas.add(dummyParentDataItem);
  </code></pre>
  
  <p>but obviously the app crash because that don't know which is the last paren't item to which add the child. 
  When i try to put both parent and child when i'm adding food from recyclerView with:</p>
  
  <pre><code>                dummyParentDataItem = new ItemPTERM();
                  dummyParentDataItem.setButton_name(filteredList.get(position).getDeskS());
                  dummyParentDataItem.setQuant( Integer.parseInt(filteredList.get(position).getQuant()));
                  dummyChildDataItems = new ArrayList&lt;&gt;();
  
                  dummyChildDataItem = new DummyChildDataItem();
                  dummyChildDataItem.setChildName("WITH PEPERONI");
                  dummyChildDataItems.add(dummyChildDataItem);
  
                  dummyParentDataItem.setChildDataItems(dummyChildDataItems);
                  itemCassas.add(dummyParentDataItem);
  </code></pre>
  
  <p>that obviously works but i have to add the child in a separate method to the last parent. </p>
  
  <p>How can i do it? any suggestion?
  Here is also my adapter code:</p>
  
  <pre><code>public class AdapterPTERM extends RecyclerView.Adapter&lt;AdapterPTERM.ExampleViewHolder&gt; {
  
      private ArrayList&lt;ItemPTERM&gt; mExampleList;
  
  
      @NonNull
      @Override
      public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_pterm,parent,false);
          return new ExampleViewHolder(v);
      }
  
      AdapterPTERM(ArrayList&lt;ItemPTERM&gt; exampleList){
          mExampleList = exampleList;
      }
  
      @Override
      public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
          ItemPTERM item = mExampleList.get(position);
  
          holder.desc.setText(item.getBtnName());
          holder.qta.setText(String.valueOf(item.getQuant()));
  
          int noOfChildTextViews = holder.linearLayout_childItems.getChildCount();
          int noOfChild = item.getChildDataItems().size();
  
          if (noOfChild &lt; noOfChildTextViews) {
              for (int index = noOfChild; index &lt; noOfChildTextViews; index++) {
                  TextView currentTextView = (TextView) holder.linearLayout_childItems.getChildAt(index);
                  currentTextView.setVisibility(View.GONE);
              }
          }
          for (int textViewIndex = 0; textViewIndex &lt; noOfChild; textViewIndex++) {
              TextView currentTextView = (TextView) holder.linearLayout_childItems.getChildAt(textViewIndex);
              currentTextView.setText(item.getChildDataItems().get(textViewIndex).getChildName());
          }
  
          if(position % 2 == 0 ){
              holder.itemView.setBackgroundColor(Color.parseColor("#C0C0C0"));
  
          }else if(position % 2 == 1){
              holder.itemView.setBackgroundColor(Color.parseColor("#D3D3D3"));
          }
  
      }
  
      @Override
      public int getItemCount() {
          return mExampleList.size();
      }
  
      public class ExampleViewHolder extends RecyclerView.ViewHolder {
  
          public TextView desc;
          public TextView qta;
          private LinearLayout linearLayout_childItems;
          private Context context;
  
          ExampleViewHolder(View itemView) {
              super(itemView);
  
              desc = itemView.findViewById(R.id.Desc);
              qta = itemView.findViewById(R.id.Qta);
              linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
              context = itemView.getContext();
  
              int intMaxNoOfChild = 0;
              for (int index = 0; index &lt; mExampleList.size(); index++) {
                  int intMaxSizeTemp = mExampleList.get(index).getChildDataItems().size();
                  if (intMaxSizeTemp &gt; intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;
              }
              for (int indexView = 0; indexView &lt; intMaxNoOfChild; indexView++) {
                  TextView textView = new TextView(context);
                  textView.setId(indexView);
                  textView.setPadding(0, 20, 0, 20);
                  textView.setGravity(Gravity.CENTER);
                  textView.setBackground(ContextCompat.getDrawable(context, R.drawable.background_sub_module_text));
                  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                  linearLayout_childItems.addView(textView, layoutParams);
              }
  
          }
      }
  
      public void removeItem(int position) {
          mExampleList.remove(position);
          notifyItemRemoved(position);
      }
  
  
  }
  </code></pre>
  
  <p>Here is a screenshot of my activity</p>
  
  <p><a href="https://i.stack.imgur.com/qvDQK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qvDQK.png" alt="enter image description here"></a></p>