将 ID 从适配器传递到新的 activity 但得到 null

Passing ID from adapter to new activity but getting null

所以我有一个适配器,它包含一个 id,我称之为 petID,我想将这个 id 传递给一个新的 activity。我已经按照以下堆栈溢出 来完成此操作,但我没有传递 id 值,而是得到了 null。我不太确定我还缺少什么。

我的适配器:

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)
        }
    }
}

我的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()
    }

}

提前致谢。

在上下文和包的构造函数中添加 private val 以便在 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)
}