Faizan
Faizan

Reputation: 25

Problem in FirebaseDatabase.getInstance().reference

I am trying to make a simple todo list android app using kotlin and firebase realtime database.. Here is 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()
}

give me an error unresolved reference: DatabaseReference and unresolved reference: FloatingActionButton

Here is the code for xml(ListView and 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" />

here is gradle dependencies(both)

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'

}

Where did I go wrong??? Any help would be appreciated

Upvotes: 1

Views: 2046

Answers (3)

Harsh Patil
Harsh Patil

Reputation: 1

In your Project Goto Tools-->Firebase
It will open an Assistant Then Perform Following Steps :

1. Select RealTime Database
2. Click on Save and Retrieve data
3. Connect Your App to Firebase
4. Add the RealtimeDatabase to Your App

Upvotes: 0

Domin
Domin

Reputation: 1145

You are missing core dependencies and need to add SDK in Build.gradle. Check out Google Documentation with instructions:
https://firebase.google.com/docs/android/setup
and hence it seems you are willing to use realtime database add also this dependency:
implementation 'com.google.firebase:firebase-database:16.0.4'

Upvotes: 1

Alex Mamo
Alex Mamo

Reputation: 138824

In order to be able to use Firebase realtime database, you need to add to your build.gradle file, the corresponding dependency:

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

Google services is also needed to be added as the last line in your file:

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

In this case, the following message:

unresolved reference: DatabaseReference

Will disappear. Now to be able to use this line of code:

mDatabase = FirebaseDatabase.getInstance().reference

Two imports are needed:

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

Regarding the second one:

unresolved reference: FloatingActionButton

This is the Design Support Floating Action Button supplied by Google that should be added:

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

It also means that you need to add an import, which is:

import android.support.design.widget.FloatingActionButton;

Upvotes: 3

Related Questions