I have debugged this and it seems to be correctly linked and all data is saved as it should but when comes time to add to the server it doesn't do anything ##
What are some things that I am doing wrong and what can I change for it to work? This is written in kotlin with a firebase real-time database and yes, I have changed the permissions in firebase to be a able to read and write
class AddItemToDataBasePage : AppCompatActivity() {
private lateinit var etName : EditText
private lateinit var etBrand : EditText
private lateinit var etType : EditText
private lateinit var etPrice : EditText
private lateinit var etAmount: EditText
lateinit var addBtn : Button
private lateinit var dbRef : DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_item_to_data_base_page)
//Linking to var to buttons so we cab get the data
etName = findViewById(R.id.addItemName)
etBrand = findViewById(R.id.addItemBrand)
etType = findViewById(R.id.addItemType)
etPrice = findViewById(R.id.addItemPrice)
etAmount = findViewById(R.id.addItemAmount)
addBtn = findViewById(R.id.addItemButton)
// DB Reference
dbRef = FirebaseDatabase.getInstance().getReference("Items")
addBtn.setOnClickListener(){
addingItems()
}
}
private fun addingItems() {
val name = etName.text.toString()
val brand = etBrand.text.toString()
val type = etType.text.toString()
val price = etPrice.text.toString()
val amount = etAmount.text.toString()
if ( name.isEmpty() && brand.isEmpty() && type.isEmpty() && price.isEmpty() && amount.isEmpty()){
Toast.makeText(this,"Please fill all areas", Toast.LENGTH_LONG).show()
}
if ( name.isNotEmpty() && brand.isNotEmpty() && type.isNotEmpty() && price.isNotEmpty() && amount.isNotEmpty()){
val itemsData = ItemsDataModel(name,brand,type,price,amount)
dbRef.child(name).setValue(itemsData).addOnSuccessListener {
etName.text.clear()
etBrand.text.clear()
etType.text.clear()
etPrice.text.clear()
etAmount.text.clear()
Toast.makeText(applicationContext,"Item Added Successfully",Toast.LENGTH_LONG).show()
}.addOnFailureListener{
Toast.makeText(applicationContext,"Failed To add Item",Toast.LENGTH_LONG).show()
}
}
}
}
These are my gradle dependencies
dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.annotation:annotation:1.4.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'com.google.firebase:firebase-database-ktx:20.0.6'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation platform('com.google.firebase:firebase-bom:30.2.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:30.4.1')
// Add the dependency for the Firebase Authentication library
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-auth-ktx'
// Also add the dependency for the Google Play services library and specify its version
implementation 'com.google.android.gms:play-services-auth:20.3.0'
}
This is the project gradle
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.13'
}
}
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir