我一直在尝试让 Firebase 用于我的大学项目之一。我按照 Google 提供的文档进行了相应设置。
build.gradle prj
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.5'
}
build.gradle 应用
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
..........
dependencies {
//noinspection GradleCompatible
implementation platform('com.google.firebase:firebase-bom:26.3.0')
//implementation "com.android.support:support-compat:27.0.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-database:19.7.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
这是我用来访问 Firebase 的代码,它位于 Object SportModel
object SportModel: Observable() {
private var mValueDataListener: ValueEventListener
private var sportList: ArrayList<Sport>? = ArrayList()
private fun getDatabaseRef():DatabaseReference{
return FirebaseDatabase.getInstance().reference.child("sports")
}
init{
if(mValueDataListener != null){
getDatabaseRef()?.removeEventListener(mValueDataListener!!)
}
Log.i("SportModel","data init 24 line")
mValueDataListener = object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
Log.i("SportModel", "data updated line 27")
val data: ArrayList<Sport> = ArrayList()
if(snapshot != null){
for(snapshot:DataSnapshot in snapshot.children){
try{
data.add(Sport(snapshot))
}
catch(e:Exception){
e.printStackTrace()
Log.i("SportModel", "37, failed to insert into the list")
}
}
sportList = data
Log.i("SportModel", "Data updated, there are ${sportList!!.size} entrees in the cache")
setChanged()
notifyObservers()
}
}
override fun onCancelled(error: DatabaseError) {
if(error != null){
Log.i("SportModel", "Data update canceled")
}
}
}
getDatabaseRef().addValueEventListener(mValueDataListener as ValueEventListener)
}
fun getData(): ArrayList<Sport>? {
return sportList
}
}
我在片段的 OnCreateView 方法中创建了 SportModel。 MainFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//Instantiate SportModel
SportModel
SportModel.addObserver(this)
currActivity = requireActivity()
val view = inflater.inflate(R.layout.fragment_main, container, false)
//create an empty data array
val data:ArrayList<Sport> = ArrayList()
adapter = SportAdapter(view.context,R.layout.sport_item, data)
val listView:ListView = view.findViewById(R.id.sport_list)
listView.adapter = adapter
//ADDED IN ORDER TO TEST IF FIREBASE WORKS AT ALL
var ref:DatabaseReference = FirebaseDatabase.getInstance().reference
ref.setValue("data123")
var mValueDataListener = object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
Log.i("SportModel", "WORKS")
}
override fun onCancelled(error: DatabaseError) {
if(error != null){
Log.i("SportModel", "Data update canceled")
}
}
}
ref.addValueEventListener(mValueDataListener)
return view
}
而且我在同一个 Fragment 中有一个被覆盖的 update 方法
override fun update(o: Observable?, arg: Any?) {
adapter?.clear()
val data = SportModel.getData()
if(data!= null){
adapter?.clear()
adapter?.addAll(data)
adapter?.notifyDataSetChanged()
}
}
根据我的理解,ValueEventListener 的 onDataChange 函数在初始化或数据更改时触发(duhhh)。出于某种原因,即使在我写的简单测试代码中,这个功能也没有得到使用。我也尝试写信给firebase。当我这样做时,在我的测试代码和实际代码中都会调用 onDataChange 函数(这应该意味着数据发生了变化???),但没有实际数据添加到 firebase 中,也没有从中提取任何数据。
我尝试从两个不同的帐户设置 firebase,但效果相同。
也许我遗漏了一些非常简单的东西?
如果您需要更多代码,请告诉我!