提问人:Md Mushaddek Al Mubdi 提问时间:9/5/2023 更新时间:9/5/2023 访问量:26
如何将 RecycleView 中的图像等 JSON 数据解析为新的活动 Kotlin
how to parse json data like image in recycleview into new activity kotlin
问:
我将 https://dummyjson.com/products 中的 json 数据解析为 recycleview 。它显示产品列表。然后我在项目上设置了 onclcik 侦听器,并将图像和描述、价格解析为新活动。价格和描述被解析为新的活动 bt 图像未显示
MyAdapter.kt (英语)
package com.fiham.retroapi
import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
class MyAdapter (val context: Activity , val productArrayList : List<Product>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder> (){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemview = LayoutInflater.from(context).inflate(R.layout.single_item, parent, false)
return MyViewHolder(itemview )
}
override fun getItemCount(): Int {
return productArrayList.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem = productArrayList[position]
holder.title.text = currentItem.title
holder.rating.text = currentItem.rating.toString()
//imageview
Picasso.get().load(currentItem.thumbnail).into(holder.image)
holder.constraint_row.setOnClickListener {
val cont = holder.constraint_row.context
val intent = Intent(it.context, ProductDetails::class.java)
intent.putExtra("image" , currentItem.thumbnail)
intent.putExtra("price" , currentItem.price)
intent.putExtra("description" , currentItem.description)
it.context.startActivity(intent)
}
}
class MyViewHolder (itemview : View) : RecyclerView.ViewHolder (itemview) {
var title : TextView
var rating : TextView
var image : ImageView
var constraint_row : ConstraintLayout = itemview.findViewById(R.id.constraint_row)
init {
title = itemview.findViewById(R.id.tv2)
image = itemview.findViewById(R.id.iv1)
rating = itemview.findViewById(R.id.rating)
}
}
}
产品.kt
package com.fiham.retroapi
data class Product(
val brand: String,
val category: String,
val description: String,
val discountPercentage: Double,
val id: Int,
val images: List<String>,
val price: Int,
val rating: Double,
val stock: Int,
val thumbnail: String,
val title: String
)
产品详情.kt
package com.fiham.retroapi
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
class ProductDetails : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_details)
val thumbnail = findViewById<ImageView>(R.id.thumbnail)
val instock = findViewById<TextView>(R.id.instock)
val desc = findViewById<TextView>(R.id.desc)
val intent = intent
val image = intent?.getIntExtra("image" , 0 )
val price = intent?.getIntExtra("price" ,0)
val description = intent?.getStringExtra("description" )
if (image != null) {
thumbnail.setImageResource(image).toString()
}
instock.text = price.toString()
desc.text = description
}
}
我想将图像解析为来自我的适配器的新活动
答:
0赞
Ibrahem Hamdy
9/5/2023
#1
当我们传递一种类型的数据时,我们必须接收相同类型的数据
问题在于,您从类型传递数据并从另一个类型接收此数据
MyAdapter.kt (英语)
holder.constraint_row.setOnClickListener {
val cont = holder.constraint_row.context
val intent = Intent(it.context, ProductDetails::class.java)
//currentItem.thumbnail is string in Prodcut.kt
intent.putExtra("image" , currentItem.thumbnail)
intent.putExtra("price" , currentItem.price)
intent.putExtra("description" , currentItem.description)
it.context.startActivity(intent)
}
产品详情.kt
class ProductDetails : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_details)
val thumbnail = findViewById<ImageView>(R.id.thumbnail)
val instock = findViewById<TextView>(R.id.instock)
val desc = findViewById<TextView>(R.id.desc)
val intent = intent
//We receive data of the same type
val image = intent?.getStringExtra("image")
val price = intent?.getIntExtra("price" ,0)
val description = intent?.getStringExtra("description" )
if (image != null) {
//The image is not in the drawble folder until we use this
// thumbnail.setImageResource(image).toString()
//We are receiving a link that we want to display this image
//from the api
//imageview
Picasso.get().load(image).into(thumbnail)
}
instock.text = price.toString()
desc.text = description
}
}
0赞
Rahul Routh
9/5/2023
#2
你必须使用它
Glide.with(context).load(image).placeholder(R.drawable.no_image).into(thumbnail)
注意R.drawable.no_image是默认图片,当没有获取任何数据时,则默认显示此图片
评论
1赞
Md Mushaddek Al Mubdi
9/6/2023
多谢。。。。现在我使用毕加索
评论