一旦我從 Firestore 下載了一些資料,我試圖讓 RecyclerView 更新,但當資料更改時它似乎沒有更新 UI。以下是主要活動:
class MainActivity : AppCompatActivity() {
private lateinit var dbService: DatabaseManager
private var mBound: Boolean = false
private val restaurantListModel: RestaurantList by viewModels()
val data = ArrayList<RestaurantCardModel>()
val adapter = RestaurantListAdapter(data)
/** Defines callbacks for service binding, passed to bindService() */
private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
val binder = service as DatabaseManager.LocalBinder
dbService = binder.getService()
mBound = true
dbService.read_restaurants(object: MyCallback {
override fun onCallback(value: MutableList<Restaurant>) {
restaurantListModel.restList.value = value
updateView()
}
})
}
override fun onServiceDisconnected(arg0: ComponentName) {
mBound = false
}
}
fun updateView(){
for (rest in restaurantListModel.restList.value!!){
data.add(RestaurantCardModel(R.drawable.pizza_express_logo, rest.name, 5, rest.review, rest.no_review,
R.color.colorPrimary))
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Bind to LocalService
Intent(this, DatabaseManager::class.java).also { intent ->
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
// getting the recyclerview by its id
val recyclerview = findViewById<RecyclerView>(R.id.restaurant_recyclerview)
// this creates a vertical layout Manager
recyclerview.layoutManager = LinearLayoutManager(this)
// Setting the Adapter with the recyclerview
recyclerview.adapter = adapter
if(mBound) { updateView() }
}
override fun onResume() {
super.onResume()
if (mBound) { updateView() }
}
override fun onStart() {
super.onStart()
}
override fun onStop() {
super.onStop()
unbindService(connection)
mBound = false
}
}
然后你有我的資料庫管理器來從 firebase 獲取資料:
class DatabaseManager : Service() {
// Binder given to clients
private val binder = LocalBinder()
var db = FirebaseFirestore.getInstance()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
fun upload_review(review_data: String){
}
fun read_restaurants(myCallback : MyCallback): MutableList<Restaurant> {
var restList : MutableList<Restaurant> = mutableListOf()
db.collection("restaurant")
.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
restList = updateRestaurantList(task.result!!)
myCallback.onCallback(restList)
} else {
Log.w(TAG, "Error getting documents.", task.exception)
}
}
return restList
}
private fun updateRestaurantList(restaurants: QuerySnapshot): MutableList<Restaurant> {
var restList : MutableList<Restaurant> = mutableListOf()
for (doc in restaurants.documents){
Log.d(TAG, doc.toString())
var tempRest : Restaurant? = doc.toObject(Restaurant::class.java)?.withId(doc.id)
if (tempRest != null) {
restList.add(tempRest)
}
}
return restList
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
inner class LocalBinder : Binder() {
// Return this instance of LocalService so clients can call public methods
fun getService(): DatabaseManager = this@DatabaseManager
}
override fun onBind(intent: Intent): IBinder {
return binder
}
}
當我在 onCreate() 方法中手動添加資料時,回收器視圖作業正常,但在 firebase 完成后不更新視圖,任何幫助將不勝感激。
uj5u.com熱心網友回復:
當我在 onCreate() 方法中手動添加資料但不更新視圖時,回收器視圖作業正常
這是預期的行為,因為您正在使用.get()呼叫,這基本上意味著您只獲取一次資料。您正在尋找的是監聽實時更新。所以你必須使用addSnapshotListener()call 。
除此之外,還有一個名為FirebaseUI for Android的有用庫,它具有Cloud Firestore的特定用途。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361497.html
標籤:安卓 火力基地 科特林 谷歌云firestore
