在这个项目中,我必须使一个可获取Firebase数据的RecyclerView。当有人单击RecyclerView中的任何特定项时,我想用从Firebase获取的相同数据打开DetailActivity。我知道这很简单,并且可以使用意图来实现。但我是新来科特林的。请帮我解决这个问题。
下面我附上了我的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)
}
}
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)
}
}
我想传递数据的活动DetailActivity.kt
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
}
}
假设您想要发送姓名、地址和电话号码来详细说明您的活动,在Java中,你可以在itemclick Listener
中这样做//从模型类中获取值
intent.putExtra("name", model.get(position).getName);
intent.putExtra("address", model.get(position).getAddress);
intent.putExtra("phone", model.get(position).getPhone);
然后在DetailsActivity上获取意图并在各自的视图中设置值。
SendingActivity
Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
intent.putExtra("keyName", value); // pass your values and retrieve them in the other Activity using keyName
startActivity(intent);
RecievingActivity
Bundle extras = intent.getExtras();
if(extras != null)
String data = extras.getString("keyName"); // retrieve the data using keyName
在项目中"SendingActivity"必须被"productivity"所取代。和"ReceivingActivity"必须由"DetailActivity"代替。