Я пытаюсь открыть изображение по URL-адресу Json, и, нажимая на это изображение, я хочу, чтобы оно открылось, но вместо этого открывается самое первое в списке. Мне нужна помощь, чтобы получить позицию, по которой щелкнули, и отобразить то же изображение.
Я попытался передать идентификатор как намерение дополнительно и использовать его во втором упражнении с помощью viewPager.currentItem, но, похоже, это неработай так.
Это запрос на выборку Json: URL Json: "http://www.infobugs.com/books/getbooklist.php?name=abh"
private fun jsonrequest() {
println("attempting to fetch json ")
request = JsonArrayRequest(jsonUrl,
Response.Listener<JSONArray> { response ->
var jsonObject: JSONObject?
for (i in 0 until response.length()) {
try {
jsonObject = response.getJSONObject(i)
listBook?.add(
i, FetchData(
jsonObject!!.getString("name"),
jsonObject.getInt("ID"),
jsonObject.getString("Sr_No"),
jsonObject.getString("path"),
jsonObject.getString("count")
)
)
println(response)
} catch (e: JSONException) {
e.printStackTrace()
}
}
setuprecyclerview(listBook)
}, Response.ErrorListener { })
Это мой адаптер для утилизации, в котором он отображается:
class RecyclerViewAdapter(
private val mContext: Context,
private val mData: ArrayList<FetchData>/*, val JSONarrayList: ArrayList<FetchData.PictureModel>*/) :
RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
private var option: RequestOptions =
RequestOptions().fitCenter().placeholder(R.drawable.loading_shape)
.error(R.drawable.loading_shape)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val inflater = LayoutInflater.from(mContext)
val view = inflater.inflate(R.layout.book_row_item, parent, false)
val viewHolder = MyViewHolder(view)
viewHolder.view_container.setOnClickListener {
val i = Intent(mContext, BookActivity::class.java)
i.putExtra("book_ID", mData[viewHolder.adapterPosition].iD)
i.putParcelableArrayListExtra("data", mData)
mContext.startActivity(i)
}
return viewHolder
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.book_name.text = mData[position].name
// Load Image from the internet and set it into Imageview using Glide
println(mData[position].path)
val uploadCurrentUrl = mData[position].path
Glide.with(mContext).load("http://$uploadCurrentUrl").apply(option).into(holder.book_path)
}
override fun getItemCount(): Int {
return mData.count()
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var book_name: TextView = itemView.findViewById(R.id.book_name)
var book_path: ImageView = itemView.findViewById(R.id.thumbnail)
var view_container: GridLayout = itemView.findViewById(R.id.container)
}
}
Это второе занятие, в котором я хочу, чтобы оно отображалось:
val data = intent.getParcelableArrayListExtra<FetchData>("data")
val viewPager : ViewPager = findViewById(R.id.viewPager)
viewPager.offscreenPageLimit = 3
val adapter = NewAdapter(this, data)
viewPager.adapter = adapter
Это адаптер ViewPager, который я использую:
class NewAdapter internal constructor(private val context: Context, private val data: ArrayList<FetchData?>
) : PagerAdapter() {
override fun getCount(): Int {
return data.size
}
override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view === `object`
}
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val itemView = LayoutInflater.from(container.context)
.inflate(R.layout.custom_book_images, container, false)
val imageView = itemView.findViewById<ImageView>(R.id.imageView)
Picasso.get()
.load("http://" + data[position]?.path)
.fit()
.centerCrop()
.into(imageView)
container.addView(itemView)
return itemView
}
override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as ConstraintLayout)
}
}
Мой ожидаемый результат:открывается точное изображение, на котором вы щелкнули, но открывается только первое. PS Я новичок и немного плохо умею кодировать, поэтому, пожалуйста, помогите ...