title: Add ArrayLIst to Recyclerview
body: <p>I have an <code>AlertDialog</code> in it there used to be a <code>ListView</code> in which the contents of the <code>ArrayList</code> were displayed. For beauty, I decided to replace it with <code>Recyclerview, Card</code> and the problems started.</p>
  <p>Items are added to <code>ArrayList &lt;String&gt; mainListWord</code> during the work and they should be included in the Recyclerview cards. The problem is that either the entire list falls into one card at once, or each next card contains the previous elements + 1. For example: the first card: 1 element, the second card: 2 elements, etc.</p>
  <p>How to implement it so that when a new element is added to the array, it will be placed in its own card?</p>
  <p><strong>activity_main.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  
     &lt;EditText
         android:id=&quot;@+id/innerText&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;50dp&quot;
         android:layout_centerInParent=&quot;true&quot;/&gt;
  
      &lt;Button
          android:id=&quot;@+id/press&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginTop=&quot;10dp&quot;
          android:text=&quot;Press&quot;
          android:layout_below=&quot;@id/innerText&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:onClick=&quot;openDialog&quot;/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>recycler_item.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
      &lt;androidx.cardview.widget.CardView
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;wrap_content&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      app:cardCornerRadius=&quot;4dp&quot;
      android:layout_margin=&quot;4dp&quot;
      app:cardBackgroundColor=&quot;@color/colorCardBack&quot;&gt;
  
      &lt;RelativeLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:layout_margin=&quot;5dp&quot;
          android:background=&quot;@color/colorCard&quot;&gt;
  
          &lt;ImageView
           android:id=&quot;@+id/imageView_1&quot;
           android:layout_width=&quot;50dp&quot;
           android:layout_height=&quot;50dp&quot;
           android:padding=&quot;4dp&quot; /&gt;
              &lt;TextView
              android:id=&quot;@+id/textview_1&quot;
              android:layout_width=&quot;wrap_content&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:text=&quot;Text_1&quot;
              android:textStyle=&quot;bold&quot;
              android:textColor=&quot;@android:color/black&quot;
              android:layout_centerInParent=&quot;true&quot;
              android:textSize=&quot;16sp&quot;
              android:layout_marginStart=&quot;5dp&quot;
              android:layout_toEndOf=&quot;@id/imageView_1&quot;/&gt;
  
  &lt;/RelativeLayout&gt;
  &lt;/androidx.cardview.widget.CardView&gt;
  </code></pre>
  <p><strong>stats_fragment.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recycler_view&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot;
          android:padding=&quot;50dp&quot;
          android:layout_margin=&quot;10dp&quot;
          android:background=&quot;@color/colorAccent&quot;
          android:scrollbars=&quot;vertical&quot;
          android:layout_above=&quot;@id/butClose&quot;/&gt;
  
      &lt;Button
          android:id=&quot;@+id/butClose&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginBottom=&quot;5dp&quot;
          android:layout_centerHorizontal=&quot;true&quot;
          android:layout_alignParentBottom=&quot;true&quot;/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private EditText innerText;
      private Button press, butClose;
  
      private ArrayList&lt;RecyclerViewItem&gt; recyclerViewItem;
      private ArrayList&lt;String&gt; mainListWord;
      private AlertDialog OptionDialog;
  
          private RecyclerView recyclerView;
          private RecyclerView.Adapter adapter;
          private RecyclerView.LayoutManager layoutManager;
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          recyclerViewItem =  new ArrayList&lt;&gt;();
          mainListWord =  new ArrayList&lt;&gt;();
  
          innerText = findViewById(R.id.innerText);
          press = findViewById(R.id.butClose);
  
  
      }
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      public void makeRecyclerList(ArrayList&lt;String&gt; income){
         String[] listWord_lenght = income.toArray(new String[0]);
         String keyWord = (String.join(&quot;&quot;, listWord_lenght));
          recyclerViewItem.add(new RecyclerViewItem(R.drawable.star, keyWord));
  
      }
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      public void openDialog(View v){
          String word = innerText.getText().toString();
          mainListWord.add(word);
              makeRecyclerList(mainListWord);
          Dialogus();
          innerText.setText(&quot;&quot;);
      }
  
      public void Dialogus(){
  
          LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View v = li.inflate(R.layout.stats_fragment, null, false);
              OptionDialog = new AlertDialog.Builder(this).create();
              OptionDialog.setTitle(&quot;TestInfo&quot;);
                  recyclerView = v.findViewById(R.id.recycler_view);
                  recyclerView.setHasFixedSize(true);
                  adapter = new RecyclerViewAdapter(recyclerViewItem);
                  layoutManager = new LinearLayoutManager(this);
                  recyclerView.setAdapter(adapter);
                  recyclerView.setLayoutManager(layoutManager);
                      butClose =  v.findViewById(R.id.butClose);
                          OptionDialog.setView(v);
                          OptionDialog.setCancelable(true);
                              butClose.setBackgroundColor(Color.CYAN);
                              butClose.setOnClickListener(new View.OnClickListener() {
                                  public void onClick(View v) {
                                      OptionDialog.dismiss();
                                  }
                              });
                              OptionDialog.show();
              }
  }
  </code></pre>
  <p><strong>RecyclerViewAdapter.java</strong></p>
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.RecyclerViewViewHolder&gt; {
  
      private ArrayList&lt;RecyclerViewItem&gt; arrayList;
  
      public static class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
  
  
  
  
      public ImageView imageView_1;
      public TextView textview_1;
  
  
          public RecyclerViewViewHolder(@NonNull View itemView) {
              super(itemView);
              imageView_1 = itemView.findViewById(R.id.imageView_1);
              textview_1 = itemView.findViewById(R.id.textview_1);
          }
      }
  
  
      public RecyclerViewAdapter(ArrayList&lt;RecyclerViewItem&gt; arrayList){
          this.arrayList = arrayList;
      }
  
      @NonNull
      @Override
      public RecyclerViewViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
  
          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_item, viewGroup, false);
              RecyclerViewViewHolder recyclerViewViewHolder= new RecyclerViewViewHolder(view);
              return  recyclerViewViewHolder;
  
      }
  
      @Override
      public void onBindViewHolder(@NonNull RecyclerViewViewHolder recyclerViewViewHolder, int position) {
          RecyclerViewItem recyclerViewItem = arrayList.get(position);
              recyclerViewViewHolder.imageView_1.setImageResource(recyclerViewItem.getImageResource());
              recyclerViewViewHolder.textview_1.setText(recyclerViewItem.getText_1());
      }
  
      @Override
      public int getItemCount() {
          return arrayList.size();
      }
      
  }
  </code></pre>
  <p><strong>RecyclerViewItem.java</strong></p>
  <pre><code>package freijer.app.one;
  
  public class RecyclerViewItem {
  
      private int imageResource;
      private String text_1;
  
      public int getImageResource() {
          return imageResource;
      }
      public void setImageResource(int imageResource) {
          this.imageResource = imageResource;
      }
          public String getText_1() {
              return text_1;
          }
          public void setText_1(String text_1) {
              this.text_1 = text_1;
          }
  
  
      public RecyclerViewItem(int imageResource, String text_1) {
          this.imageResource = imageResource;
          this.text_1 = text_1;
      }
  }
  </code></pre>