2024-04-26 19:57:44 +08:00
|
|
|
|
package com.zjgsu.jianshu
|
2024-04-30 09:41:24 +08:00
|
|
|
|
import android.content.Context
|
2024-04-27 14:00:50 +08:00
|
|
|
|
import android.content.Intent
|
2024-04-26 19:57:44 +08:00
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.text.TextUtils
|
2024-04-27 14:00:50 +08:00
|
|
|
|
import android.util.Log
|
2024-04-26 19:57:44 +08:00
|
|
|
|
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
|
2024-04-27 14:00:50 +08:00
|
|
|
|
lateinit var currentPosition: Pair<Int, Int>
|
2024-04-26 19:57:44 +08:00
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
setContentView(R.layout.activity_bookrank)
|
|
|
|
|
Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
|
2024-04-30 09:41:24 +08:00
|
|
|
|
val userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
|
|
|
|
|
adapter= BookRankAdapter(this,hotbookList,true,userId)
|
2024-04-26 19:57:44 +08:00
|
|
|
|
bookrank_recyclerView.layoutManager=StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)
|
|
|
|
|
bookrank_recyclerView.adapter=adapter
|
|
|
|
|
bookrank_swipeRefresh.setOnRefreshListener {
|
|
|
|
|
loadBookRanks() //下拉刷新
|
|
|
|
|
}
|
|
|
|
|
loadBookRanks()
|
2024-04-27 14:00:50 +08:00
|
|
|
|
bookrank_title.text="一周热门图书榜单"
|
|
|
|
|
bookrank_subtitle.text="10本・ 8213人关注"
|
|
|
|
|
returnTOCity()
|
2024-04-26 19:57:44 +08:00
|
|
|
|
}
|
|
|
|
|
private fun loadBookRanks(){
|
|
|
|
|
val queryHotBooks = BmobQuery<Book_info_bmob>()
|
|
|
|
|
queryHotBooks.order("-countOfReaders") // 根据 countOfReaders 降序排序
|
2024-04-27 14:00:50 +08:00
|
|
|
|
queryHotBooks.setLimit(10) // 限制查询结果的数量为10
|
2024-04-26 19:57:44 +08:00
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-04-27 14:00:50 +08:00
|
|
|
|
//TODO:未来添加一个返回到上个页面指定位置的功能
|
|
|
|
|
private fun returnTOCity() {
|
|
|
|
|
bookrank_return.setOnClickListener {
|
|
|
|
|
val intent = Intent(this, MainActivity::class.java)
|
|
|
|
|
startActivity(intent)
|
|
|
|
|
finish() // 结束当前页面
|
2024-04-26 19:57:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|