Search code examples
androidkotlinandroid-recyclerviewadapter

Passing ID from adapter to new activity but getting null


So I have an adapter which holds an id, which I call petID, and I want to pass this id to a new activity. I've followed the following stack overflow post to accomplish this but rather than passing the id value, I am getting null. I'm not too sure what else I am missing.

my adapter:

class PetProfilesAdapter(private val mList: List<PetProfilesData>, context: Context, bundle: Bundle) : RecyclerView.Adapter<PetProfilesAdapter.ViewHolder>() {
private val context = context
private val bundle = bundle
//create new views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    //inflate the pet_profile view, which is used to hold the list item
    val view = LayoutInflater.from(parent.context).inflate(R.layout.pet_profile, parent, false)
    return ViewHolder(view, context, bundle)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    val PetProfilesData = mList[position]

    // sets the text to the textview from our itemHolder class
    holder.textView.text = PetProfilesData.text

    holder.id = PetProfilesData.id

}

// return the number of the items in the list
override fun getItemCount(): Int {
    return mList.size
}

// Holds the views for adding it to image and text
class ViewHolder(ItemView: View, context: Context, bundle: Bundle) : RecyclerView.ViewHolder(ItemView) {
    val textView: TextView = itemView.findViewById(R.id.petProfile)
    lateinit var id: String
    init {
        textView.setOnClickListener {
            Toast.makeText(it.context, "ID is " + id, Toast.LENGTH_LONG).show()
            val intent = Intent(context, RemindersActivity::class.java)
            intent.putExtra("petID", id)
            startActivity(context, intent, bundle)
        }
    }
}

my activity:

class AddReminderActivity : AppCompatActivity() {

val db = Firebase.firestore
lateinit var petID: String
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add_reminder)
    val editText: EditText = findViewById(R.id.reminder_title)
    val reminderTitle = editText.text.toString()

    val timePicker: TimePicker = findViewById(R.id.reminder_time)
    val datePicker: DatePicker = findViewById(R.id.reminder_date)

    val spinner: Spinner = findViewById(R.id.reminder_frequency)
    ArrayAdapter.createFromResource(
        this,
        R.array.frequency,
        android.R.layout.simple_spinner_item
    ).also { adapter ->
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = adapter
    }


    val saveButton: Button = findViewById(R.id.reminder_save_button)
    saveButton.setOnClickListener{
        val combinedCal = Calendar.getInstance()
        combinedCal.set(datePicker.year, datePicker.month, datePicker.dayOfMonth, timePicker.hour, timePicker.minute)
        val timestamp = Timestamp(combinedCal.timeInMillis)

        val frequency = spinner.selectedItem
        petID = intent.getStringExtra("petID").toString()

        val reminder = hashMapOf(
            "title" to reminderTitle,
            "timestamp" to timestamp,
            "frequency" to frequency
        )

        Toast.makeText(it.context, petID, Toast.LENGTH_LONG).show()
    }

}

Thanks in advance.


Solution

  • Add private val in constructor for context and bundle for making them accessible in onBindingViewHolder

      class PetProfilesAdapter(private val mList: List<PetProfilesData>, private val context: Context, private val bundle: Bundle) : RecyclerView.Adapter<PetProfilesAdapter.ViewHolder>() {
    .
    .
    .
    .
    .
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
      
        val petProfilesData = mList[position] //use camelCasing for variable naming
    
        holder.textView.text = petProfilesData.text
    
        holder.textView.setonClickListener {
            Toast.makeText(it.context, "ID is " + pteProfilesData.id, Toast.LENGTH_LONG).show()
            val intent = Intent(context, RemindersActivity::class.java)
            intent.putExtra("petID", petProfilesData.id)
            startActivity(context, intent, bundle)
        }
    
    }
    
    
    class ViewHolder(ItemView: View, context: Context, bundle: Bundle) : RecyclerView.ViewHolder(ItemView) {
        val textView: TextView = itemView.findViewById(R.id.petProfile)
    }