title: Add new cardview dynamically on button click in android
body: <p>I am using a cardview with recyclerview with hardcoded values such as string arrays. But now i want to add new cardview on each button click with user enter values and user select image and keep remain all cardviews on app exit.I mean to say that cardview should be added one by one only.Any solution plz guide me.</p>
  
  <p>cardview Adapter class</p>
  
  <pre><code>public class CardViewDataAdapter extends RecyclerView.Adapter&lt;CardViewDataAdapter.ViewHolder&gt; {
  
  private static ArrayList&lt;FeddProperties&gt; dataSet;
  private static Context context;
  
  public CardViewDataAdapter(Context context, ArrayList&lt;FeddProperties&gt; os_versions) {
      this.context = context;
      dataSet = os_versions;
  }
  
  
  @Override
  public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  
      View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
              R.layout.card_view, null);
  
      // create ViewHolder
  
      ViewHolder viewHolder = new ViewHolder(itemLayoutView);
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {
  
      FeddProperties fp = dataSet.get(i);
  
      viewHolder.vehicleNumber.setText(fp.getTitle());
      viewHolder.iconView.setImageResource(fp.getThumbnail());
      viewHolder.feed = fp;
  }
  
  @Override
  public int getItemCount() {
      return dataSet.size();
  }
  
  
  public static class ViewHolder extends RecyclerView.ViewHolder {
  
      public TextView vehicleNumber;
      public ImageView iconView;
      public FeddProperties feed;
  
      public ViewHolder(View itemLayoutView) {
          super(itemLayoutView);
  
          vehicleNumber = (TextView) itemLayoutView
                  .findViewById(R.id.vehiclenumber);
          iconView = (ImageView) itemLayoutView
                  .findViewById(R.id.iconId);
  
          itemLayoutView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent = new Intent(v.getContext(), InformingUser.class);
                  v.getContext().startActivity(intent);
  
                  ((MainActivity) context).getSupportFragmentManager().beginTransaction().replace
                          (R.id.containerView, new InformingUser()).commit();
  
                  //Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
              }
          });
      }
  
  }
  </code></pre>
  
  <p>Java file....</p>
  
  <pre><code>private void initContrls()
  {
      SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, 0);
      vehiclecatory = prefs.getString("vehicle_category", "");
      vehicletype = prefs.getString("vehicle_type", "");
  
      String versions =  prefs.getString("city", "")+" "+prefs.getString("dis", "")+" "+prefs.getString("number", "");
      String vehicleCompany = prefs.getString("company", "")+" "+prefs.getString("model", "");
  
      if(vehiclecatory.equals("1"))
      {
          if (vehicletype.equals("1"))
          {
              icons = R.drawable.contwowheel;
          }
          else if (vehicletype.equals("2"))
          {
              icons = R.drawable.comfourwheel;
          }
  
          else if (vehicletype.equals("3"))
          {
              icons = R.drawable.comheavy;
          }
      }
      else if (vehiclecatory.equals("2"))
      {
          if (vehicletype.equals("1"))
          {
              icons = R.drawable.nontwowheel;
          }
          else if (vehicletype.equals("2"))
          {
              icons = R.drawable.nonfourwheel;
          }
          else if (vehicletype.equals("3"))
          {
              icons = R.drawable.nonheavy;
          }
      }
  
      os_versions = new ArrayList&lt;FeddProperties&gt;();
      for (int i = 0; i &lt; 2; i++) {
          FeddProperties feed = new FeddProperties();
  
          feed.setTitle(versions);
          feed.setVehicleCompany(vehicleCompany);
          feed.setThumbnail(icons);
          os_versions.add(feed);
      }
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(new LinearLayoutManager(context));
      mAdapter = new CardViewDataAdapter(context,os_versions);
      recyclerView.setAdapter(mAdapter);
  }
  </code></pre>