title: Dynamic cardviews combined with firebase
body: <p>I'm having a list of <code>CardViews</code>. Each <code>CardView</code> (representing a challenge) has a <code>ViewPager</code> to show a vertical scrolable list of "solutions". </p>
  
  <p>The data and images in the "solutions list" is coming from a <code>Firebase</code> database. 
  <strong>The issue that I have is that the first CardView you open</strong> (when you click on it, it expands and shows the solutions) <strong>displays all the solutions correctly. When you click on the second one, it expands but no data is shown and I think it is like this because I have to work with a "final" layout</strong> ( <code>final FrameLayout v0 = (FrameLayout) inflater.inflate(R.layout.solutions, null);</code>)</p>
  
  <p><a href="https://i.stack.imgur.com/UqJGXm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UqJGXm.png" alt="enter image description here"></a></p>
  
  <p>This below is a snippet of the important parts of the custom <code>RecyclerView</code> adapter.</p>
  
  <pre><code>public class ChallengesAdapter extends 
  RecyclerView.Adapter&lt;ChallengesAdapter.RecyclerItemViewHolder&gt; {
  private ArrayList&lt;ChallengesData&gt; myChallengesList;
  int mLastPosition = 0;
  
  //Firebase
  String mUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
  DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
  DatabaseReference challengeSolutionsRef = rootRef.child("challenges");
  
  FirebaseStorage storage = FirebaseStorage.getInstance();
  StorageReference storageRef = storage.getReferenceFromUrl("firebase-url");
  
  public ChallengesAdapter(ArrayList&lt;ChallengesData&gt; myChallengesList) {
      this.myChallengesList = myChallengesList;
  }
  public RecyclerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenges_row, parent, false);
      RecyclerItemViewHolder holder = new RecyclerItemViewHolder(view);
      return holder;
  }
  @Override
  public void onBindViewHolder(RecyclerItemViewHolder holder, final int position) {
      holder.etTitleTextView.setText(myChallengesList.get(position).getTitle());
      holder.etDescriptionTextView.setText(myChallengesList.get(position).getDescription());
      mLastPosition =position;
  }
  @Override
  public int getItemCount() {
      return(null != myChallengesList ? myChallengesList.size():0);
  }
  public void notifyData(ArrayList&lt;ChallengesData&gt; myList) {
      this.myChallengesList = myList;
      notifyDataSetChanged();
  }
  public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
      private final TextView etTitleTextView;
      private final TextView etDescriptionTextView;
      //private final TextView etContent;
      private final ChallengesPagerAdapter myChallengesPagerAdapter;
      private final ViewPager myViewPager;
      private final FrameLayout myFrameLayout;
      private LinearLayout mainLayout;
  
      public RecyclerItemViewHolder(final View parent) {
          super(parent);
          etTitleTextView = (TextView) parent.findViewById(R.id.txtTitle);
          etDescriptionTextView = (TextView) parent.findViewById(R.id.txtDescription);
          myViewPager = (ViewPager) parent.findViewById(R.id.view_pager);
          myChallengesPagerAdapter = new ChallengesPagerAdapter();
          myViewPager.setAdapter(myChallengesPagerAdapter);
          myFrameLayout = (FrameLayout) parent.findViewById(R.id.framelayout_view_pager);
          mainLayout = (LinearLayout) parent.findViewById(R.id.mainLayout);
  
          mainLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  challengeSolutionsRef = challengeSolutionsRef.child(myChallengesList.get(getAdapterPosition()).getChallengeKey()).child("solutions");
                  challengeSolutionsRef.addChildEventListener(new ChildEventListener() {
                      @Override
                      public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
                          final LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
                          DatabaseReference solutionsRef = rootRef.child("solutions").child(dataSnapshot.getKey());
                          solutionsRef.addValueEventListener(new ValueEventListener() {
                              @Override
                              public void onDataChange(DataSnapshot dataSnapshot) {
                                  final FrameLayout v0 = (FrameLayout) inflater.inflate(R.layout.solutions, null);
                                  TextView TVSolutionID = (TextView)v0.findViewById(R.id.txtSolutionID);
                                  TextView TVUserID = (TextView)v0.findViewById(R.id.txtUserID);
                                  TVSolutionID.setText(dataSnapshot.getKey());
                                  String UserID = dataSnapshot.child("userId").getValue(String.class);
                                  TVUserID.setText(UserID);
  
                                  myChallengesPagerAdapter.notifyDataSetChanged();
  
                                  //get file from cloud
                                  storageRef.child("solutions/").child(dataSnapshot.child("images").getValue(String.class) + ".jpg").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener&lt;byte[]&gt;() {
                                      @Override
                                      public void onSuccess(byte[] bytes) {
                                          // Use the bytes to display the image
                                          Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                                          ImageView IVSolution = (ImageView)v0.findViewById(R.id.ivSolutionImage);
                                          IVSolution.setImageBitmap(bitmap);
                                          myChallengesPagerAdapter.notifyDataSetChanged();
                                      }
                                  }).addOnFailureListener(new OnFailureListener() {
                                      @Override
                                      public void onFailure(@NonNull Exception exception) {
                                          ImageView IVSolution = (ImageView)v0.findViewById(R.id.ivSolutionImage);
                                          IVSolution.setImageResource(R.mipmap.ic_challenger_black);
                                          myChallengesPagerAdapter.notifyDataSetChanged();
                                      }
                                  });
                                  myChallengesPagerAdapter.addView(v0);
                                  myChallengesPagerAdapter.notifyDataSetChanged();
                              }
                              @Override
                              public void onCancelled(DatabaseError databaseError) {
                                  Log.d("Test","The read failed: " + databaseError.getCode());
                              }
  
                          });
                      }
  
                          ....
  
                  });
  
                  ....
              }
          });
      }
  }
  }
  </code></pre>
  
  <p><strong>This is the challenges_row.xml:</strong></p>
  
  <pre><code>&lt;android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_margin="8dp"
  android:id="@+id/card_view"
  android:layout_height="wrap_content"&gt;
  &lt;LinearLayout
      android:orientation="vertical"
      android:id="@+id/mainLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
      &lt;TextView
          android:id="@+id/txtTitle"
          android:padding="12dp"
          android:layout_width="match_parent"
          android:textColor="@android:color/black"
          android:layout_height="wrap_content" /&gt;
      &lt;TextView
          android:id="@+id/txtDescription"
          android:padding="12dp"
          android:layout_width="match_parent"
          android:textColor="@android:color/black"
          android:layout_height="wrap_content" /&gt;
      &lt;FrameLayout
          android:id="@+id/framelayout_view_pager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:visibility="gone"&gt;
  
          &lt;android.support.v4.view.ViewPager
              android:id="@+id/view_pager"
              android:layout_width="match_parent"
              android:layout_height="300dp"&gt;
          &lt;/android.support.v4.view.ViewPager&gt;
          &lt;ImageButton
              android:id="@+id/arrow_back"
              android:layout_width="48dp"
              android:layout_height="48dp"
              android:layout_gravity="center_vertical|left"
              android:src="@drawable/ic_arrow_back" /&gt;
  
          &lt;ImageButton
              android:id="@+id/arrow_forward"
              android:layout_width="48dp"
              android:layout_height="48dp"
              android:layout_gravity="center_vertical|right"
              android:src="@drawable/ic_arrow_forward" /&gt;
  &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>and this is solutions.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="wrap_content"&gt;
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
          &lt;TextView
              android:id="@+id/txtSolutionID"
              android:padding="12dp"
              android:layout_width="match_parent"
              android:textColor="@android:color/black"
              android:layout_height="wrap_content"/&gt;
          &lt;TextView
              android:id="@+id/txtUserID"
              android:padding="12dp"
              android:layout_width="match_parent"
              android:textColor="@android:color/black"
              android:layout_height="wrap_content"/&gt;
          &lt;ImageView
              android:id="@+id/ivSolutionImage"
              android:layout_width="match_parent"
              android:layout_height="match_parent" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p><strong>Edit with advise from Marcos: (still same result)</strong></p>
  
  <pre><code>public class ChallengesAdapter extends RecyclerView.Adapter&lt;ChallengesAdapter.RecyclerItemViewHolder&gt; {
  private ArrayList&lt;ChallengesData&gt; myChallengesList;
  int mLastPosition = 0;
  
  //Firebase
  String mUID = FirebaseAuth.getInstance().getCurrentUser().getUid();
  DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
  DatabaseReference challengeSolutionsRef = rootRef.child("challenges");
  
  FirebaseStorage storage = FirebaseStorage.getInstance();
  StorageReference storageRef = storage.getReferenceFromUrl("firebase-url");    
  
  public ChallengesAdapter(ArrayList&lt;ChallengesData&gt; myChallengesList) {
      this.myChallengesList = myChallengesList;
  }
  public RecyclerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenges_row, parent, false);
      RecyclerItemViewHolder holder = new RecyclerItemViewHolder(view);
      return holder;
  }
  @Override
  public void onBindViewHolder(final RecyclerItemViewHolder holder, final int position) {
      holder.etTitleTextView.setText(myChallengesList.get(position).getTitle());
      holder.etDescriptionTextView.setText(myChallengesList.get(position).getDescription());
      mLastPosition =position;
  
      holder.mainLayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Toast.makeText(holder.itemView.getContext(), "Position:" + myChallengesList.get(holder.getAdapterPosition()).getDescription(), Toast.LENGTH_SHORT).show();
              //create reference to solutions inside challenges data
              challengeSolutionsRef = challengeSolutionsRef.child(myChallengesList.get(holder.getAdapterPosition()).getChallengeKey()).child("solutions");
              challengeSolutionsRef.addChildEventListener(new ChildEventListener() {
                  @Override
                  public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
  
                      DatabaseReference solutionsRef = rootRef.child("solutions").child(dataSnapshot.getKey());
                      solutionsRef.addValueEventListener(new ValueEventListener() {
                          @Override
                          public void onDataChange(DataSnapshot dataSnapshot) {
                              holder.v0 = (FrameLayout) holder.myInflater.inflate(R.layout.solutions, null);
                              TextView TVSolutionID = (TextView)holder.v0.findViewById(R.id.txtSolutionID);
                              TextView TVUserID = (TextView)holder.v0.findViewById(R.id.txtUserID);
                              TVSolutionID.setText(dataSnapshot.getKey());
                              String UserID = dataSnapshot.child("userId").getValue(String.class);
                              TVUserID.setText(UserID);
  
                              holder.myChallengesPagerAdapter.notifyDataSetChanged();
  
                              storageRef.child("Solutions/").child(dataSnapshot.child("picture").getValue("filename").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener&lt;byte[]&gt;() {
                                  @Override
                                  public void onSuccess(byte[] bytes) {
                                      // Use the bytes to display the image
                                      Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                                      ImageView IVSolution = (ImageView)holder.v0.findViewById(R.id.ivSolutionImage);
                                      IVSolution.setImageBitmap(bitmap);
                                      holder.myChallengesPagerAdapter.notifyDataSetChanged();
                                  }
                              }).addOnFailureListener(new OnFailureListener() {
                                  @Override
                                  public void onFailure(@NonNull Exception exception) {
                                      // Handle any errors
                                      //Create new item in the rowItems arraylist to add to the listview
                                      ImageView IVSolution = (ImageView)holder.v0.findViewById(R.id.ivSolutionImage);
                                      IVSolution.setImageResource(R.mipmap.ic_challenger_black);
                                      holder.myChallengesPagerAdapter.notifyDataSetChanged();
                                  }
                              });
                              holder.myChallengesPagerAdapter.addView(holder.v0);
                              holder.myChallengesPagerAdapter.notifyDataSetChanged();
                          }
                          @Override
                          public void onCancelled(DatabaseError databaseError) {
                              Log.d("Test","The read failed: " + databaseError.getCode());
                          }
  
                      });
                  }
   ...
  
              });
  
      });
  
  }
  @Override
  public int getItemCount() {
      return(null != myChallengesList ? myChallengesList.size():0);
  }
  public void notifyData(ArrayList&lt;ChallengesData&gt; myList) {
      this.myChallengesList = myList;
      notifyDataSetChanged();
  }
  public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
      private TextView etTitleTextView;
      private TextView etDescriptionTextView;
      //private final TextView etContent;
      private ChallengesPagerAdapter myChallengesPagerAdapter;
      private ViewPager myViewPager;
      private FrameLayout myFrameLayout;
      private LinearLayout mainLayout;
      private View myParent;
      private LayoutInflater myInflater;
      private FrameLayout v0;
      public RecyclerItemViewHolder(final View parent) {
          super(parent);
          etTitleTextView = (TextView) parent.findViewById(R.id.txtTitle);
          etDescriptionTextView = (TextView) parent.findViewById(R.id.txtDescription);
          myViewPager = (ViewPager) parent.findViewById(R.id.view_pager);
          myChallengesPagerAdapter = new ChallengesPagerAdapter();
          myViewPager.setAdapter(myChallengesPagerAdapter);
          myFrameLayout = (FrameLayout) parent.findViewById(R.id.framelayout_view_pager);
          mainLayout = (LinearLayout) parent.findViewById(R.id.mainLayout);
          myParent = parent;
          myInflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      }
  }
  }
  </code></pre>