title: (Android Kotlin) How to retrieve the image "Name" from firebase storage on a Button click in a recyclerView?
body: <p><strong>Goal:</strong> <br>
  To retrieve the default image "name" from the images present in firebase storage, on click of a button, such that the name can be stored in a local txt file. (which will be used to filter out images later on).</p>
  
  <p><strong>Setup so Far:</strong> </p>
  
  <ul>
  <li>Have created the recyclerView which is displaying images from the firebase storage in a cardView.</li>
  <li>Each card in the recyclerView contains only the image and a button, "doneButton".</li>
  <li>Have setup the function to save strings in a local txt file.</li>
  </ul>
  
  <p><strong>Where I'm Stuck:</strong><br></p>
  
  <ul>
  <li><p>Not able to retrieve the image name from storage on button click,
  both of which (button &amp; image) are present on a card in the
  recyclerView.</p></li>
  <li><p>Once the name is retrieved, on the same button click action, need to pass
  the image name to the save image name function to    write it to a
  local txt file.</p></li>
  </ul>
  
  <p>Would be really helpful if someone can help out with this. Thank you. (FYI: I'm new to android development)</p>
  
  <p><br>
  <strong>Main Activity:</strong></p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_numbers_tab)
  
  
      //firebase
      val storage = FirebaseStorage.getInstance()
      val storageRef = storage.reference.child("Images")
      val imageList:ArrayList&lt;CoronaImages&gt; = ArrayList()
  
  
      val listAllTask: Task&lt;ListResult&gt; = storageRef.listAll()
      listAllTask.addOnCompleteListener{ result -&gt;
          val items: List&lt;StorageReference&gt; = result.result!!.items
  
          //cycle for adding image URL to list
          items.forEachIndexed { index, item -&gt;
              item.downloadUrl.addOnSuccessListener {
                  Log.d("item","$it")
                  imageList.add(CoronaImages(it.toString()))
              }.addOnCompleteListener{
  
                  ImageRecyclerView.adapter = ImageAdapter(imageList, this)
                  ImageRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true)
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Adapter</strong> (with an attempt to create the OnClickListener for image name):</p>
  
  <pre><code>class ImageAdapter (
          private var coronaImage: List&lt;CoronaImages&gt;, private val context: Context):
          RecyclerView.Adapter&lt;ImageAdapter.ViewHolder&gt;(){
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
          return ViewHolder(
              LayoutInflater.from(context).inflate(R.layout.corona_images, parent, false)
          )
      }
  
  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      val item = coronaImage[position]
      Picasso.get()
          .load(item.imageUrl)
          .into(holder.imageView)
  }
  
  override fun getItemCount(): Int {
      return coronaImage.size
  }
  
  class ViewHolder(view: View):RecyclerView.ViewHolder(view), View.OnClickListener {
          val imageView: ImageView=view.findViewById(R.id.imageView1)
  
  
  // this is where I'm stuck:
          val doneButton: CardView=view.findViewById(R.id.doneButton)
  
          init {
              doneButton.setOnClickListener {
                  object : View.OnClickListener {
                          override fun onClick(v: View?) {
  
  
               // how can I retrieve the image name and pass it to the writeToFile ()??
  
  
                          writeToFile(????)
                          }
                      }
                  }
              }
  
              // Function to Write to a File
              fun writeToFile(FirebaseImageName: String) {
              try {
                  var writeData = FileWriter("doneCoronaImages.txt", true)
                  writeData.write(FirebaseImageName +"\n")
                  writeData.close()
              }
              catch (ex: Exception) {
                  print("Could not save the image name.")
              }
          }
      }
  }
  </code></pre>
  
  <p>Data Class for Image URLs:</p>
  
  <pre><code>data class CoronaImages(
      var imageUrl: String
  )
  </code></pre>
  
  <p>coronaImage XML</p>
  
  <pre><code>&lt;com.google.android.material.card.MaterialCardView
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/card"
      android:layout_width="match_parent"
      android:layout_height="455dp"
      android:layout_margin="8dp"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"&gt;
  
          &lt;!-- Media --&gt;
          &lt;ImageView
              android:id="@+id/imageView1"
              android:layout_width="match_parent"
              android:layout_height="390dp"
              android:scaleType="centerCrop" /&gt;
  
          &lt;!-- Buttons --&gt;
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="8dp"
              android:orientation="horizontal"&gt;
  
              &lt;Button
                  android:id="@+id/coronaButton"               style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
                  android:layout_width="90sp"
                  android:layout_height="55sp"
                  android:layout_marginStart="0dp"
                  android:layout_marginEnd="8dp"
                  android:layout_marginBottom="0dp"
                  android:text="@string/coronaButtonTitle"
                  android:textSize="16sp"/&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/com.google.android.material.card.MaterialCardView&gt;
  </code></pre>