Home > database >  How can I pass the data from RecyclerView (ProductActivity) to DetailActivity?
How can I pass the data from RecyclerView (ProductActivity) to DetailActivity?

Time:06-27

In this project, I have to make a RecyclerView that will fetch the Firebase data. When someone clicks on any particular item in the RecyclerView, I want to open DetailActivity with the same data fetched from the Firebase. I know this is easy and can be done using intent.putExtra but I'm new here in Kotlin. Help me with the solution, please.

Below I'm attaching my code of ProductActivity.kt.

class ProductActivity() : AppCompatActivity(), ProductAdapter.OnItemClickListener
{
private var adapter: ProductAdapter? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_product)

    val query = FirebaseDatabase.getInstance().reference.child("products")
    val options = FirebaseRecyclerOptions.Builder<Product>().setQuery(query, 
    Product::class.java).build()
    adapter = ProductAdapter(options, this)
    val rView : RecyclerView = findViewById(R.id.rView)
    rView.layoutManager = LinearLayoutManager(this)
    rView.adapter = adapter
}

override fun onStart() {
    super.onStart()
    adapter?.startListening()
}

override fun onItemClick(position: Int) {
    Toast.makeText(this, "Item $position clicked", Toast.LENGTH_SHORT).show()
    val intent = Intent(this, DetailActivity::class.java)
    startActivity(intent)
 }
}

The code of ProductAdapter.kt

class ProductAdapter(options: FirebaseRecyclerOptions<Product>, private val listener: OnItemClickListener)
: FirebaseRecyclerAdapter<Product, ProductAdapter.MyViewHolder>(options) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    return MyViewHolder(inflater, parent)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int, model: Product) {

    val storRef: StorageReference =
        FirebaseStorage.getInstance().getReferenceFromUrl(model.photo)
    Glide.with(holder.imgPhoto.context).load(storRef).into(holder.imgPhoto)
    holder.txtName.text = model.name
    holder.txtPrice.text = model.price
    holder.txtDescription.text = model.description
}

inner class MyViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
    RecyclerView.ViewHolder(inflater.inflate(R.layout.row_layout, parent, false)), View.OnClickListener {
    val txtName: TextView = itemView.findViewById(R.id.productName)
    val txtPrice: TextView = itemView.findViewById(R.id.productPrice)
    val txtDescription: TextView = itemView.findViewById(R.id.productDescription)
    val imgPhoto: ImageView = itemView.findViewById(R.id.productImage)

    init {
        itemView.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        val position: Int = adapterPosition
        if (position != RecyclerView.NO_POSITION) {
            listener.onItemClick(position)
        }
    }
}

interface OnItemClickListener {
    fun onItemClick(position: Int)
  }
}

The activity in which I want to pass data DetailActivity.kt

class DetailActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_detail)

  }
}

CodePudding user response:

Suppose you want to send name, address, and phone number to detail your activity, in Java you would do like this inside the itemclick Listener

//Get the values from the model class

  intent.PutExtra("name", model.get(position).getName);
    intent.PutExtra("address", model.get(position).getAddress);
    intent.PutExtra("phone", model.get(position).getPhone);

then on the DetailsActivity get the intent and set the values in their respective views.

CodePudding user response:

You Can Use "parcable class" Or "MutableLiveData" Or "Rxjava".

  • Related