FirebaseDatabase.getInstance()。reference中的问题

时间:2018-11-18 11:08:24

标签: android firebase kotlin

我正在尝试使用kotlin和firebase实时数据库制作一个简单的待办事项列表android应用。这是mainActivity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import android.widget.EditText
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)


    // reference for FAB
    val fab = findViewById<View>(R.id.fab) as FloatingActionButton

    //Adding click listener for FAB
    fab.setOnClickListener { view ->

        //Show Dialog here to add new Item
        addNewItemDialog()

    }

    lateinit var mDatabase: DatabaseReference
    mDatabase = FirebaseDatabase.getInstance().reference

}

class ToDoItem {

    companion object Factory {
        fun create(): ToDoItem = ToDoItem()
    }

    var objectId: String? = null
    var itemText: String? = null
    var done: Boolean? = false
}

object Constants {
    @JvmStatic val FIREBASE_ITEM: String = "todo_item"
}

private fun addNewItemDialog(){

    val alert = AlertDialog.Builder(this)

    val itemEditText = EditText(this)
    alert.setMessage("Add New Item")
    alert.setTitle("Enter To Do Item Text")

    alert.setView(itemEditText)

    alert.setPositiveButton("Submit"){ dialog, positiveButton ->

        val todoItem = ToDoItem.create()
        todoItem.itemText = itemEditText.text.toString()
        todoItem.done = false

        //we first make a push so that a new item is made with a unique id

        val newItem = mDatabase.child(Constants.FIREBASE_ITEM).push()
        todoItem.objectId = newItem.key

        //then, we used the reference to set the value on that ID
        newItem.setValue(todoItem)

        dialog.dismiss()
        Toast.makeText(this, "Item saved with ID" + todoItem.objectId,Toast.LENGTH_SHORT).show()

    }

    alert.show()
}

给我一​​个错误的未解决参考:DatabaseReference和未解决的参考:FloatingActionButton

这是xml(ListView和FloatingActionButton)的代码

<ListView
        android:id="@+id/items_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:scrollbars="none" />

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="15dp"
        android:src="@android:drawable/ic_input_add"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp" />

这是gradle依赖项(两者)

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.0.1'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
kapt 'com.google.firebase:firebase-core:16.0.5'

}

我哪里出错了???任何帮助将不胜感激

3 个答案:

答案 0 :(得分:2)

为了能够使用Firebase实时数据库,您需要向您的build.gradle文件中添加相应的依赖项:

implementation 'com.google.firebase:firebase-database:16.0.5'

还需要将Google服务添加为文件的最后一行:

apply plugin: 'com.google.gms.google-services'

在这种情况下,以下消息:

unresolved reference: DatabaseReference

会消失。现在可以使用以下代码行:

mDatabase = FirebaseDatabase.getInstance().reference

需要两次导入:

import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.DatabaseReference;

关于第二个:

unresolved reference: FloatingActionButton

这是Google提供的“设计支持浮动操作”按钮,应添加:

implementation 'com.android.support:design:27.1.1'

这还意味着您需要添加一个导入,

import android.support.design.widget.FloatingActionButton;

答案 1 :(得分:1)

您缺少核心依赖性,需要在Build.gradle中添加SDK。 查阅Google文档中的说明:
 https://firebase.google.com/docs/android/setup
因此似乎您愿意使用实时数据库并添加此依赖项:
implementation 'com.google.firebase:firebase-database:16.0.4'

答案 2 :(得分:0)

在您的项目中 转到工具-> Firebase
它将打开一个助手 然后执行以下步骤:

1。选择实时数据库
2.单击保存并检索数据
3.将您的应用程序连接到Firebase
4.将RealtimeDatabase添加到您的应用程序