87 lines
3.8 KiB
Kotlin
87 lines
3.8 KiB
Kotlin
package com.zjgsu.jianshu
|
||
import android.content.Context
|
||
import android.content.Intent
|
||
import android.os.Bundle
|
||
import android.text.TextUtils
|
||
import android.util.Log
|
||
import android.widget.SearchView
|
||
import android.widget.Toast
|
||
import androidx.appcompat.app.AppCompatActivity
|
||
import androidx.recyclerview.widget.GridLayoutManager
|
||
import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||
import cn.bmob.v3.Bmob
|
||
import cn.bmob.v3.BmobQuery
|
||
import cn.bmob.v3.exception.BmobException
|
||
import cn.bmob.v3.listener.FindListener
|
||
import com.zjgsu.jianshu.*
|
||
import com.zjgsu.jianshu.Adapter.BillboardAdapter
|
||
import com.zjgsu.jianshu.Adapter.BookRankAdapter
|
||
import com.zjgsu.jianshu.Bmob.Book_info_bmob
|
||
import kotlinx.android.synthetic.main.activity_bookrank.*
|
||
import kotlinx.android.synthetic.main.activity_main.*
|
||
|
||
class HotBookActivity: AppCompatActivity() {
|
||
private var hotbookList=ArrayList<Book_rank>()
|
||
lateinit var adapter:BookRankAdapter
|
||
lateinit var currentPosition: Pair<Int, Int>
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
setContentView(R.layout.activity_bookrank)
|
||
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
|
||
val userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
|
||
adapter= BookRankAdapter(this,hotbookList,true,userId)
|
||
bookrank_recyclerView.layoutManager=StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
|
||
bookrank_recyclerView.adapter=adapter
|
||
bookrank_swipeRefresh.setOnRefreshListener {
|
||
loadBookRanks() //下拉刷新
|
||
}
|
||
loadBookRanks()
|
||
bookrank_title.text="一周热门图书榜单"
|
||
bookrank_subtitle.text="10本・ 8213人关注"
|
||
returnTOCity()
|
||
}
|
||
private fun loadBookRanks(){
|
||
val queryHotBooks = BmobQuery<Book_info_bmob>()
|
||
queryHotBooks.order("-countOfReaders") // 根据 countOfReaders 降序排序
|
||
queryHotBooks.setLimit(10) // 限制查询结果的数量为10
|
||
queryHotBooks.findObjects(object : FindListener<Book_info_bmob>() {
|
||
override fun done(books: List<Book_info_bmob>?, e: BmobException?) {
|
||
bookrank_swipeRefresh.isRefreshing = false
|
||
if (e == null) {
|
||
// 检查 books 是否非空
|
||
if (books != null && books.isNotEmpty()) {
|
||
hotbookList.clear() // 清空当前列表
|
||
// 遍历 books 并转换为 Book_rank 类型后添加到列表
|
||
books.forEach { b ->
|
||
val bookRank = Book_rank(
|
||
name = b.name,
|
||
picUrl = b.picture.url,
|
||
score="0.0",
|
||
courtOfReaders = b.countOfReaders,
|
||
author=b.author_name,
|
||
intro=b.introduce
|
||
)
|
||
hotbookList.add(bookRank)
|
||
adapter.notifyDataSetChanged() // 确保在这里更新适配器
|
||
}
|
||
}
|
||
} else {
|
||
// 处理错误情况
|
||
Toast.makeText(
|
||
this@HotBookActivity,
|
||
"Failed to retrieve books: ${e.message}",
|
||
Toast.LENGTH_SHORT
|
||
).show()
|
||
}
|
||
}
|
||
})
|
||
}
|
||
//TODO:未来添加一个返回到上个页面指定位置的功能
|
||
private fun returnTOCity() {
|
||
bookrank_return.setOnClickListener {
|
||
val intent = Intent(this, MainActivity::class.java)
|
||
startActivity(intent)
|
||
finish() // 结束当前页面
|
||
}
|
||
}
|
||
} |