95 lines
4.1 KiB
Kotlin
95 lines
4.1 KiB
Kotlin
|
package com.zjgsu.jianshu.Adapter
|
||
|
|
||
|
import com.zjgsu.jianshu.Book
|
||
|
import com.zjgsu.jianshu.Book_informationActivity
|
||
|
import com.zjgsu.jianshu.R
|
||
|
import android.content.Context
|
||
|
import android.content.Intent
|
||
|
import android.graphics.Bitmap
|
||
|
import android.graphics.BitmapFactory
|
||
|
import android.util.Log
|
||
|
import android.view.LayoutInflater
|
||
|
import android.view.View
|
||
|
import android.view.ViewGroup
|
||
|
import android.widget.ImageView
|
||
|
import android.widget.TextView
|
||
|
import androidx.appcompat.widget.ActivityChooserView
|
||
|
import androidx.core.content.ContextCompat
|
||
|
import androidx.recyclerview.widget.RecyclerView
|
||
|
import cn.bmob.v3.BmobQuery
|
||
|
import cn.bmob.v3.datatype.BmobFile
|
||
|
import cn.bmob.v3.exception.BmobException
|
||
|
import cn.bmob.v3.listener.FindListener
|
||
|
import com.bumptech.glide.Glide
|
||
|
import com.zjgsu.jianshu.Bmob.Book_bmob
|
||
|
import java.io.InputStream
|
||
|
import java.net.HttpURLConnection
|
||
|
import java.net.URL
|
||
|
|
||
|
class BookAdapter2(val bookList: List<Book>) : RecyclerView.Adapter<BookAdapter2.ViewHolder>() {
|
||
|
|
||
|
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||
|
val bookList_Image: ImageView = view.findViewById(R.id.book_Image)
|
||
|
val bookList_Name: TextView = view.findViewById(R.id.book_Name)
|
||
|
val bookList_Introduce: TextView = view.findViewById(R.id.book_Introduce)
|
||
|
}
|
||
|
|
||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||
|
val view = View.inflate(parent.context, R.layout.book_item, null)
|
||
|
return ViewHolder(view)
|
||
|
}
|
||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||
|
val book = bookList[position]
|
||
|
holder.bookList_Name.text = book.name
|
||
|
holder.bookList_Introduce.text = book.introduce
|
||
|
val query = BmobQuery<Book_bmob>()
|
||
|
query.addWhereEqualTo("name", book.name)
|
||
|
query.findObjects(object : FindListener<Book_bmob>() {
|
||
|
override fun done(p0: MutableList<Book_bmob>?, p1: BmobException?) {
|
||
|
if (p1 == null) {
|
||
|
if (p0 != null && p0.size > 0) {
|
||
|
for (p in p0) {
|
||
|
object : Thread() {
|
||
|
override fun run() {
|
||
|
try {
|
||
|
val url = URL(p!!.picture.url)
|
||
|
val connection: HttpURLConnection =
|
||
|
url.openConnection() as HttpURLConnection
|
||
|
connection.setRequestMethod("GET")
|
||
|
connection.setConnectTimeout(5000)
|
||
|
val `in`: InputStream = connection.getInputStream()
|
||
|
val bitmap: Bitmap =
|
||
|
BitmapFactory.decodeStream(`in`)
|
||
|
holder.bookList_Image.setImageBitmap(bitmap)
|
||
|
} catch (e: Exception) {
|
||
|
e.printStackTrace()
|
||
|
}
|
||
|
}
|
||
|
}.start()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
// Glide.with(context).load(book.imageId).into(holder.bookList_Image)
|
||
|
|
||
|
holder.bookList_Image.setOnClickListener {
|
||
|
val intent = Intent(holder.itemView.context, Book_informationActivity::class.java)
|
||
|
intent.putExtra("Book_name", book.name)
|
||
|
ContextCompat.startActivity(holder.itemView.context, intent, null)
|
||
|
}
|
||
|
holder.bookList_Introduce.setOnClickListener {
|
||
|
val intent = Intent(holder.itemView.context, Book_informationActivity::class.java)
|
||
|
intent.putExtra("Book_name", book.name)
|
||
|
ContextCompat.startActivity(holder.itemView.context, intent, null)
|
||
|
}
|
||
|
holder.bookList_Name.setOnClickListener {
|
||
|
val intent = Intent(holder.itemView.context, Book_informationActivity::class.java)
|
||
|
intent.putExtra("Book_name", book.name)
|
||
|
ContextCompat.startActivity(holder.itemView.context, intent, null)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
override fun getItemCount() = bookList.size
|
||
|
}
|