how i can store different urls and each url acts differently when clicked by the user
This is data model class
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.laptopguide.databinding.ActivityMainBinding
import com.example.laptopguide.databinding.ActivityWindowsLaptopBinding
class WindowsLaptop : AppCompatActivity() {
private lateinit var binding: ActivityWindowsLaptopBinding
private lateinit var laptopAdapter: LaptopAdapter
private lateinit var dataList:ArrayList<ItemsViewModel>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityWindowsLaptopBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
supportActionBar?.hide()
dataList = ArrayList<ItemsViewModel>()
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)", "54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)", "54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)","54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)","54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)","54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)","54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)","54999", "Buy Now"))
dataList.add(ItemsViewModel(R.drawable.images, "Ideapad Gaming 3", "i5 12th Generation (16 GB/512 GB SSD)", "54999", "Buy Now"))
laptopAdapter = LaptopAdapter(dataList, this)
binding.rvLaptop.layoutManager = LinearLayoutManager(this)
binding.rvLaptop.adapter = laptopAdapter
}
}
Like this is a laptop guide application user can see the laptop and when user decide to buy a laptop i want to redirect user to amazon via the link so every laptop have their own url so how i can store and use those urls for specific button
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.laptopguide.databinding.LaptopDesignBinding
class LaptopAdapter(var dataList:ArrayList<ItemsViewModel>, var context:Context):RecyclerView.Adapter<LaptopAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LaptopAdapter.ViewHolder {
var binding = LaptopDesignBinding.inflate(LayoutInflater.from(context), parent, false)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return dataList.size
}
override fun onBindViewHolder(holder: LaptopAdapter.ViewHolder, position: Int) {
anima(holder.itemView)
holder.binding.ivImage.setImageResource(dataList.get(position).laptopImage)
holder.binding.tvName.text = dataList.get(position).laptopName
holder.binding.tvSpecs.text = dataList.get(position).laptopSpecs
holder.binding.tvPrice.text = dataList.get(position).laptopPrice
holder.binding.btnBuy.text = dataList.get(position).laptopBuy
holder.binding.btnShare.setOnClickListener {
var intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT,"Share this")
val chooser =Intent.createChooser(intent, "Share This Laptop Using....")
context.startActivity(chooser)
}
holder.binding.btnBuy.setOnClickListener(View.OnClickListener(){
val intent = Intent(android.content.Intent.ACTION_VIEW)
intent.data = Uri.parse("")
context.startActivity(intent)
})
}
inner class ViewHolder(var binding:LaptopDesignBinding): RecyclerView.ViewHolder(binding.root)
fun anima (view:View){
var animation= AlphaAnimation(0.0f, 1.0f)
animation.duration = 1500
view.startAnimation(animation)
}
}
Add a url field in your ItemsViewModel class
data class ItemsViewModel(
...
val url: String
)
and then in adapter you can get this url per item with just dataList.get(position).url
like:
holder.binding.btnBuy.setOnClickListener(View.OnClickListener(){
val intent = Intent(android.content.Intent.ACTION_VIEW)
intent.data = Uri.parse(dataList.get(position).url)
context.startActivity(intent)
})