When I start my app it crash and I get get Following error message in logcat:
"lateinit property newTrack has not been initialized"
I have locate the problem to my "showdata" function, but I can't see what the problem is.
class MainActivity : AppCompatActivity(), View.OnClickListener {
val collection = "song"
val artistName = "name"
val trackName = "track"
var docId =""
lateinit var newTrack : SoundTrack
lateinit var db : FirebaseFirestore
lateinit var alSongs : ArrayList<HashMap<String,Any>>
lateinit var adapter: SimpleAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
alSongs = ArrayList()
addData.setOnClickListener(this)
updateInfo.setOnClickListener(this)
deleteInfo.setOnClickListener(this)
IsData.setOnItemClickListener(itemClick)
}
override fun onStart() {
super.onStart()
db = FirebaseFirestore.getInstance()
db.collection(collection).addSnapshotListener { querySnapshot, e ->
if(e != null) Log.d("fireStore", e.message)
showData()
}
}
override fun onClick(v: View?) {
when(v?.id){
//add new data by input
R.id.addData ->{
val hm = HashMap<String, Any>()
hm.set(newTrack.name,ArtistName.text.toString())
hm.set(newTrack.track,track.text.toString())
db.collection(collection).document(ArtistName.text.toString()).set(hm).
addOnSuccessListener {
Toast.makeText(this, "Data Successfully added", Toast.LENGTH_SHORT)
.show()
}.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully added : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
//Update input
R.id.updateInfo -> {
val hm = HashMap<String, Any>()
hm.set(newTrack.name,ArtistName.text.toString())
hm.set(newTrack.track,track.text.toString())
db.collection(collection).document(docId).update(hm)
.addOnSuccessListener { Toast.makeText(this, "Data Successfully updated", Toast.LENGTH_SHORT)
.show() }
.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully updated : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
//delete Input
R.id.deleteInfo -> {
db.collection(collection).whereEqualTo(newTrack.name,docId).get().addOnSuccessListener {
results ->
for(doc in results){
db.collection(collection).document(doc.id).delete()
.addOnSuccessListener {
Toast.makeText(this, "Data Successfully updated", Toast.LENGTH_SHORT)
.show()
}.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully updated : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}.addOnFailureListener { e ->
Toast.makeText(this, "Cant get data reference: ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}
}
val itemClick = AdapterView.OnItemClickListener { parent, view, position, id ->
val hm = alSongs.get(position)
docId = hm.get(newTrack.name).toString()
ArtistName.setText(hm.get(newTrack.name).toString())
track.setText(hm.get(newTrack.name).toString())
}
//Show input data
fun showData(){
db.collection(collection).get().addOnSuccessListener { result ->
alSongs.clear()
for(doc in result){
val hm = HashMap<String,Any>()
hm.set(newTrack.name,doc.get(newTrack.name).toString())
hm.set(newTrack.track,doc.get(newTrack.track).toString())
alSongs.add(hm)
}
adapter = SimpleAdapter(this,alSongs,R.layout.row_data,
arrayOf(newTrack.name,newTrack.track),
intArrayOf(R.id.txName, R.id.TxTrack))
IsData.adapter = adapter
}
}
}
The whole point of lateinit is to prevent nullability. When you use lateinit you are saying that by the time I want to use this value it will be initialized. When you try to use it before you initialize it you get this error so you need to initialize it first
You can use if(::newTrack.isInitialized) like others have said but IMO that defeats the purpose of lateinit
You should initialize newTrack variable or check is it initialized
if(::newTrack.isInitialized){
// logic
}else{
newTrack = SoundTrack()
}
you can using .isInitialized property one can check initialization state of a lateinit variable.
if(this::newTrack.isInitialized){
//newTrack is initialized
}else{
//newTrack is not initialized
}
Related
I have used Kotlin to create a task management app using Firebase, but the problem that I am having is that, users are able to see each others tasks and their tasks are stored in the "same account". When user 1 creates a task, user 2 is able to see what user 1 has created and the task that is created by user 2 is in the same list as the one created by user 1. How do I stop/avoid this? I do not want my users to share data, I want them to have their data in their respective accounts.
This is for registration:
class RegisterPage : AppCompatActivity() {
private lateinit var binding: ActivityRegisterPageBinding
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var progressDialog: ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRegisterPageBinding.inflate(layoutInflater)
setContentView(binding.root)
firebaseAuth = FirebaseAuth.getInstance()
progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please wait")
progressDialog.setCanceledOnTouchOutside(false)
binding.backButton.setOnClickListener{
onBackPressed()
}
binding.regButton.setOnClickListener{
validateData()
}
}
private var name =""
private var surname = ""
private var email = ""
private var password = ""
private var confirmPassword = ""
private fun validateData() {
name = binding.regName.text.toString().trim()
surname = binding.regSurname.text.toString().trim()
email = binding.regEmail.text.toString().trim()
password = binding.regCreate.text.toString().trim()
confirmPassword = binding.regConfirm.text.toString().trim()
if (name.isNotEmpty() && surname.isNotEmpty() && email.isNotEmpty() && password.isNotEmpty() && confirmPassword.isNotEmpty()) {
if(password == confirmPassword){
createAccount()
}else{
Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT).show()
}
}else{
Toast.makeText(this, "Please provide missing details", Toast.LENGTH_SHORT).show()
}
}
private fun createAccount(){
progressDialog.setMessage("Creating account...")
progressDialog.show()
// Adding user to firebase
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnSuccessListener() {
updateUser()
}.addOnFailureListener{e->
progressDialog.dismiss()
Toast.makeText(this, "${e.message}", Toast.LENGTH_SHORT).show()
}
}
private fun updateUser(){
progressDialog.setMessage("Saving...")
val timestamp = System.currentTimeMillis()
// get curr user id
val uid = firebaseAuth.uid
// setup data to add in db
val hashMap: HashMap<String, Any?> = HashMap()
hashMap["uid"] = uid
hashMap["name"] = name
hashMap["surname"] = surname
hashMap["email"] = email
hashMap["timestamp"] = timestamp
hashMap["profilePicture"] = ""
hashMap["userType"] = "user"
//set data to db
val ref = FirebaseDatabase.getInstance().getReference("Users")
ref.child(uid!!).setValue(hashMap).addOnSuccessListener {
//Info saved, open home screen
progressDialog.dismiss()
Toast.makeText(this, "Account successfully created!", Toast.LENGTH_SHORT).show()
startActivity(Intent(this#RegisterPage, HomeScreen::class.java))
finish()
}.addOnFailureListener{ e ->
//failed adding data to db
progressDialog.dismiss()
Toast.makeText(this, "${e.message}", Toast.LENGTH_SHORT).show()
}
}
This is for login:
class LoginPage : AppCompatActivity() {
private lateinit var binding: ActivityLoginPageBinding
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var progressDialog: ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginPageBinding.inflate(layoutInflater)
setContentView(binding.root)
firebaseAuth = FirebaseAuth.getInstance()
progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please wait")
progressDialog.setCanceledOnTouchOutside(false)
binding.noAcc.setOnClickListener{
startActivity(Intent(this, RegisterPage::class.java))
}
binding.loginButton.setOnClickListener{
validateUserData()
}
binding.forgotPassword.setOnClickListener{
startActivity(Intent(this, ForgotPassword::class.java))
}
}
private var email = ""
private var password = ""
private fun validateUserData(){
// Input data
email = binding.loginEmail.text.toString().trim()
password = binding.loginPassword.text.toString().trim()
// Validating data
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(this, "Invalid email format", Toast.LENGTH_SHORT).show()
//Toast.makeText(this, "exception!!", Toast.LENGTH_SHORT).show()
}else if(password.isEmpty()){
Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show()
}else{
loginUser()
}
}
// Login firebase auth
private fun loginUser() {
progressDialog.setMessage("Logging in...")
progressDialog.show()
/*val timestamp = System.currentTimeMillis()
// get curr user id
val uid = firebaseAuth.uid
// setup data to add in db
val hashMap: HashMap<String, Any?> = HashMap()
hashMap["uid"] = uid
hashMap["email"] = email
hashMap["timestamp"] = timestamp
hashMap["userType"] = "user"
val ref = FirebaseDatabase.getInstance().getReference("Logins")*/
firebaseAuth.signInWithEmailAndPassword(email, password)
.addOnSuccessListener {
// Successful login
checkUser()
}.addOnFailureListener { e ->
// failed to login
progressDialog.dismiss()
Toast.makeText(this, "${e.message}", Toast.LENGTH_SHORT).show()
}
}
private fun checkUser() {
/* Check user type - Firebase Auth
* user - move to user dashboard
* admin - move to admin dashboard*/
progressDialog.setMessage("Checking account...")
val firebaseUser = firebaseAuth.currentUser!!
val ref = FirebaseDatabase.getInstance().getReference("Users")
ref.child(firebaseUser.uid).addListenerForSingleValueEvent(object: ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
progressDialog.dismiss()
// Get user type - user/admin
val userType = snapshot.child("userType").value
if(userType == "user"){
startActivity(Intent(this#LoginPage, HomeScreen::class.java))
finish()
}else if (userType == "admin"){
startActivity(Intent(this#LoginPage, HomeScreen::class.java))
finish()
}
}
override fun onCancelled(error: DatabaseError) {
}
})
}
}
I am using Firebase PhoneAuth for user registration with my app. Users are verified successfully and started using the app without any issues. But, It happened to me a couple of times that user details vanished from Firestore database, not all users details have gone though. When I check the collection users in the Firestore, the document does exist for the user but some data was gone, in fact, it's overwritten the old data as if this is new user registration (However, the User UID under the Authentication is remain same). For example, the default value is user_type = "customer", as you can see in the fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential). Later I change this value accordingly to my need and when this issue happens the changes I made to this field and other fields are changed back to the default values.
Following is the code in my SignInWithPhone which will be called from the SplashScreen
class SigninWithPhoneActivity: BaseActivity() {
private lateinit var binding: ActivitySignInWithPhoneBinding
private lateinit var mAuth: FirebaseAuth
var code = ""
var number = ""
var phoneNumber = ""
var storedOtpID = ""
private lateinit var resendToken: PhoneAuthProvider.ForceResendingToken
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignInWithPhoneBinding.inflate(layoutInflater)
setContentView(binding.root)
}
mAuth = FirebaseAuth.getInstance()
binding.btnGetOtp.setOnClickListener {
code = binding.etCountryCode.text.toString().trim()
number = binding.etMobileNumber.text.toString().trim()
phoneNumber = code + number
if (number.isNotEmpty()) {
binding.etMobileNumber.isEnabled = false
binding.flOtp.visibility = View.VISIBLE
binding.btnGetOtp.visibility = View.GONE
binding.btnSignIn.visibility = View.VISIBLE
sendVerificationCode(phoneNumber)
} else {
Toast.makeText(this, "Please enter a valid mobile number", Toast.LENGTH_LONG).show()
}
}
binding.btnSignIn.setOnClickListener {
val otp = binding.etOtp.text.toString().trim()
if (otp.isNotEmpty() || otp.length != 6) {
verifyVerificationCode(otp)
} else {
Toast.makeText(this, "Please enter the OTP received through SMS", Toast.LENGTH_LONG)
.show()
}
}
}
private val mCallBack: PhoneAuthProvider.OnVerificationStateChangedCallbacks =
object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(credential: PhoneAuthCredential) {
val code = credential.smsCode
if (code != null) {
binding.etOtp.setText(code)
binding.pbOtp.visibility = View.GONE
}
}
override fun onVerificationFailed(p0: FirebaseException) {
binding.etMobileNumber.isEnabled = true
binding.flOtp.visibility = View.GONE
binding.btnGetOtp.visibility = View.VISIBLE
binding.btnSignIn.visibility = View.GONE
Toast.makeText(this#SigninWithPhoneActivity, "Login Failed", Toast.LENGTH_LONG)
.show()
}
override fun onCodeSent(otpID: String, token: PhoneAuthProvider.ForceResendingToken) {
super.onCodeSent(otpID, token)
Toast.makeText(
this#SigninWithPhoneActivity,
"OTP is send to your number",
Toast.LENGTH_LONG
).show()
storedOtpID = otpID
resendToken = token
}
}
private fun sendVerificationCode(phoneNumber: String) {
binding.pbOtp.visibility = View.VISIBLE
Toast.makeText(this#SigninWithPhoneActivity, "Sending OTP", Toast.LENGTH_LONG).show()
val options = PhoneAuthOptions.newBuilder(mAuth!!)
.setPhoneNumber(phoneNumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(this)
.setCallbacks(mCallBack)
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
}
private fun verifyVerificationCode(code: String) {
Toast.makeText(
this#SigninWithPhoneActivity,
"Verifying credentials",
Toast.LENGTH_LONG
).show()
val credential = PhoneAuthProvider.getCredential(storedOtpID, code)
signInWithPhoneAuthCredential(credential)
}
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val firebaseUser: FirebaseUser = task.result!!.user!!
val userPreliminaryDetails = User(
firebaseUser.uid,
user_type = "customer",
mobile = binding.etMobileNumber.text.toString()
)
FirestoreClass().checkIfUserAlreadyExist(
this#SigninWithPhoneActivity,
firebaseUser.uid,
userPreliminaryDetails
)
} else {
showErrorSnackBar(task.exception!!.message.toString(), true)
if (task.exception is FirebaseAuthInvalidCredentialsException) {
binding.etMobileNumber.isEnabled = true
binding.flOtp.visibility = View.GONE
binding.etOtp.text?.clear()
binding.btnGetOtp.visibility = View.VISIBLE
binding.btnGetOtp.text = "Resend OTP"
binding.btnSignIn.visibility = View.GONE
Toast.makeText(this, "OTP entered is wrong", Toast.LENGTH_LONG).show()
}
}
}
}
fun userRegistrationSuccess() {
finish()
startActivity(Intent(this, ServiceAreaActivity::class.java))
Toast.makeText(this, "Signed Up successful", Toast.LENGTH_LONG).show()
}
fun userSignInSuccess() {
finish()
startActivity(Intent(this, ServiceAreaActivity::class.java))
Toast.makeText(this, "Signed in successfully", Toast.LENGTH_LONG).show()
}
}
EDIT:
The following function is called to check if the user already exists and accordingly sign in or register a new user.
fun checkIfUserAlreadyExist(
activity: SigninWithPhoneActivity, userId: String, userDetails: User
) {
mFireStore.collection("users")
.whereEqualTo(Constants.USER_ID, userId)
.get()
.addOnSuccessListener { document ->
if (document.documents.size > 0) {
activity.userSignInSuccess()
} else {
FirestoreClass().registerUser(activity, userDetails)
}
}
.addOnFailureListener { e ->
}
}
private fun registerUser(activity: SigninWithPhoneActivity, userInfo: User) {
mFireStore.collection(Constants.USERS)
.document(userInfo.user_id)
.set(userInfo, SetOptions.merge())
.addOnSuccessListener {
activity.userRegistrationSuccess()
}
.addOnFailureListener { e ->
activity.hideProgressDialog()
}
}
basically i have a button -->Onclick-->alert dialog is appearing ..in that alert dialog im having a ratingbar.on that rating bar im sending the rate value to server using retrofit which i have done succesfully.but what i have problem is storing that rate value using shared preference.im sharing my code following:--
ratebutton.setOnClickListener {
val mBuild: AlertDialog.Builder = AlertDialog.Builder(this#Product_details)
val mView: View = layoutInflater.inflate(R.layout.ratedialog, null)
val ratebar =mView.findViewById(R.id.ratingBaruser) as RatingBar
val titleimage=mView.findViewById<TextView>(R.id.titleimage) as TextView
val imagerate=mView.findViewById<ImageView>(R.id.imagerate) as ImageView
titleimage.setText(ygd)
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(0).image).into(imagerate);
val wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(applicationContext)
val rating: Float = wmbPreference1.getFloat("numStars", 0f)
ratebar.setRating(rating)
ratebar.onRatingBarChangeListener =
OnRatingBarChangeListener { ratingBar, rating1, fromUser ->
val editor = wmbPreference1.edit();
editor.putFloat("numStars", rating);
editor.commit();
rateValue = rating1
Toast.makeText(this#Product_details, "" + rating1, Toast.LENGTH_SHORT).show()
}
val btnSubmit =
mView.findViewById(R.id.btnSubRating) as Button
btnSubmit.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
RetrofitClient.instancetable.setRating(id,rateValue)
.enqueue(object : Callback<Rate_Response> {
override fun onFailure(call: Call<Rate_Response>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<Rate_Response>,
response: Response<Rate_Response>
) {
var res = response
if (res.isSuccessful) {
Toast.makeText(
applicationContext,
res.body()?.user_msg,
Toast.LENGTH_LONG
).show()
}
else{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
Toast.makeText(this#Product_details, "" + rateValue, Toast.LENGTH_SHORT).show()
}
})
mBuild.setView(mView)
val dialog: AlertDialog = mBuild.create()
dialog.show()
}
Above code output:--
when i again click that button i unable to see the previous rate value done by user
i need help thanks in advance
i have a edittext in Alert Dialog.....my operation is performing well if ediitext is not empty.....i want to set focus when ediitext is empty in alertdialog ..
need help thanks in advance
i tried the following code please have a look:---
buynow.setOnClickListener {
val mBuild: AlertDialog.Builder = AlertDialog.Builder(this#Product_details)
val mView: View = layoutInflater.inflate(R.layout.buynowdialog, null)
val titleimage=mView.findViewById<TextView>(R.id.titleimage2) as TextView
val imagerate=mView.findViewById<ImageView>(R.id.imagebuy) as ImageView
titleimage.setText(ygd)
Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(0).image).into(imagerate);
val buynumber = mView.findViewById<EditText>(R.id.editbuy)
val btnSubmit =
mView.findViewById(R.id.btnbuynow) as Button
Log.e("checkid",id.toString())
mBuild.setView(mView)
val dialog: AlertDialog = mBuild.create()
btnSubmit.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val value: String = buynumber.getText().toString()
val finalValue = value.toInt()
if (value.isEmpty()) {
Toast.makeText(
applicationContext, "Data is missing",Toast.LENGTH_LONG
).show()
editbuy.error = "Email required"
editbuy.requestFocus()
}
val token: String =
SharedPrefManager.getInstance(
applicationContext
).user.access_token.toString()
RetrofitClient.instancecart.buynow(token,id,finalValue)
.enqueue(object : Callback<ResponseBaseCartAdd> {
override fun onFailure(call: Call<ResponseBaseCartAdd>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<ResponseBaseCartAdd>,
response: Response<ResponseBaseCartAdd>
) {
Log.e("hi",id)
var res = response
Log.e("checkres",res.toString())
Log.d("response check ", "" + response.body()?.status.toString())
if (res.isSuccessful) {
Toast.makeText(
applicationContext,
res.body()?.user_msg,
Toast.LENGTH_LONG
).show()
dialog.dismiss()
Log.d("kjsfgxhufb",response.body()?.user_msg.toString())
}
else{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getString("message")+jObjError.getString("user_msg"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
Log.e("errorrr",e.message)
}
}
}
})
}
})
dialog.show()
}
above code is crashing with an error in logcat:-- java.lang.NumberFormatException: For input string: ""
need help thanks :)
NumberFormatException is an Exception that might be thrown when you
try to convert a String into a number, where that number might be an
int , a float , or any other Java numeric type.
val finalValue = value.toInt() // value is empty. That's why problem
Your finalValue is Empty or null. You must check it.
isNotEmpty() is used to find if the String is not empty/String is
length 0 and not null.
DEMO
if (value.isNotEmpty()) {
val finalValue = value.toInt()
val token: String =SharedPrefManager.getInstance(applicationContext).user.access_token.toString()
RetrofitClient.instancecart.buynow(token,id,finalValue)
.....your task.....
}
else
{
Toast.makeText(applicationContext, "Data is missing",Toast.LENGTH_LONG).show()
editbuy.error = "Email required"
editbuy.requestFocus()
}
I have a problem whit my code, but I get no errors. The problem arise when I try change my variables ”artistName” and ”trackName” to my object ”SoundTrack”. If I use my variables ”artistName” and ”trackName” it works fine, I can upload, delete and display data etc. But when I try o use my object nothing is happend, It’s like it doesn’t connect to my database.
My class:
class SoundTrack (val name : String, val track : String)
Here is my MainActivity:
class MainActivity : AppCompatActivity(), View.OnClickListener {
val collection = "song"
//val artistName = "name"
//val trackName = "track"
var docId =""
lateinit var newTrack : SoundTrack
lateinit var db : FirebaseFirestore
lateinit var alSongs : ArrayList<HashMap<String,Any>>
lateinit var adapter: SimpleAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
alSongs = ArrayList()
addData.setOnClickListener(this)
updateInfo.setOnClickListener(this)
deleteInfo.setOnClickListener(this)
IsData.setOnItemClickListener(itemClick)
}
override fun onStart() {
super.onStart()
db = FirebaseFirestore.getInstance()
db.collection(collection).addSnapshotListener { querySnapshot, e ->
if(e != null) Log.d("fireStore", e.message)
showData()
}
}
//add new data by input
override fun onClick(v: View?) {
when(v?.id){
R.id.addData ->{
println("hej")
if(::newTrack.isInitialized){
val hm = HashMap<String, Any>()
hm.set(newTrack.name,artistNametxt.text.toString())
hm.set(newTrack.track,trackNametxt.text.toString())
db.collection(collection).document(artistNametxt.text.toString()).set(hm).
addOnSuccessListener {
Toast.makeText(this, "Data Successfully added", Toast.LENGTH_SHORT)
.show()
}.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully added : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}
//Update input
R.id.updateInfo -> {
if(::newTrack.isInitialized){
val hm = HashMap<String, Any>()
hm.set(newTrack.name,artistNametxt.text.toString())
hm.set(newTrack.track,trackNametxt.text.toString())
db.collection(collection).document(docId).update(hm)
.addOnSuccessListener { Toast.makeText(this, "Data Successfully updated", Toast.LENGTH_SHORT)
.show() }
.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully updated : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}
//delete Input
R.id.deleteInfo -> {
if(::newTrack.isInitialized){
db.collection(collection).whereEqualTo(newTrack.name,docId).get().addOnSuccessListener {
results ->
for(doc in results){
db.collection(collection).document(doc.id).delete()
.addOnSuccessListener {
Toast.makeText(this, "Data Successfully updated", Toast.LENGTH_SHORT)
.show()
}.addOnFailureListener { e ->
Toast.makeText(this, "Data unSuccessfully updated : ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}.addOnFailureListener { e ->
Toast.makeText(this, "Cant get data reference: ${e.message}", Toast.LENGTH_SHORT)
.show()
}
}
}
}
}
val itemClick = AdapterView.OnItemClickListener { parent, view, position, id ->
val hm = alSongs.get(position)
docId = hm.get(newTrack.name).toString()
artistNametxt.setText(hm.get(newTrack.name).toString())
trackNametxt.setText(hm.get(newTrack.name).toString())
}
//Show input data
fun showData(){
if(::newTrack.isInitialized){
db.collection(collection).get().addOnSuccessListener { result ->
alSongs.clear()
for(doc in result){
val hm = HashMap<String,Any>()
hm.set(newTrack.name,doc.get(newTrack.name).toString())
hm.set(newTrack.track,doc.get(newTrack.track).toString())
alSongs.add(hm)
}
}
adapter = SimpleAdapter(this,alSongs,R.layout.row_data,
arrayOf(newTrack.name,newTrack.track),
intArrayOf(R.id.txName, R.id.TxTrack))
IsData.adapter = adapter
}
}
}
Try this
fun getSongs(): LiveData<MutableList<SongTrack>> {
firestoreInstance.collection(SONG_COLLECTION)
.addSnapshotListener { querySnapshot,
firebaseFirestoreException ->
song.clear()
querySnapshot?.documents?.forEach {document ->
val songTrack = document.toObject(SongTrack::class.java)
songTrack?.let {
song.add(it)
}
}
}
songList.postValue(song)
return songList
}
First i would change
class SoundTrack (val name : String, val track : String)
to
data class SoundTrack(val name : String ="",val track : String ="")
and delete
lateinit var newTrack : SoundTrack
so you don't need to do this anymore
if(::newTrack.isInitialized)
delete
lateinit var db : FirebaseFirestore
and create Object class
object FirestoreUtil {
private val firestoreInstance: FirebaseFirestore by lazy {
FirebaseFirestore.getInstance()
}
}
or better yet
class MainActivity : AppCompatActivity() {
lateinit var adapter: SimpleAdapter
val itemClick = AdapterView.OnItemClickListener { parent, view, position, id ->
val selectedSong = FirestoreUtils.songList.value?.get(position)
selectedSong?.let {
artistNametxt.setText(it.name.toString())
trackNametxt.setText(it.track.toString())
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirestoreUtils.getSongs().observe(this, Observer { songList ->
adapter = SimpleAdapter(
this, songList, R.layout.row_data,
arrayOf(SongTrack().name, SongTrack().track),
intArrayOf(R.id.txName, R.id.TxTrack)
)
IsData.adapter = adapter
})
addData.setOnClickListener {
FirestoreUtils.addSong(
SongTrack(
artistNametxt.text.toString(),
trackNametxt.text.toString()
)
) { message -> Toast.makeText(this, message, Toast.LENGTH_SHORT).show() }
}
updateInfo.setOnClickListener {
FirestoreUtils.updateSong(
SongTrack(
artistNametxt.text.toString(),
trackNametxt.text.toString()
)
) { message ->
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
deleteInfo.setOnClickListener(FirestoreUtils.deleteSong(artistNametxt.text.toString()) { message ->
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
})
IsData.setOnItemClickListener {
itemClick
}
}
}
const val SONG_COLLECTION = "song"
data class SongTrack(val name: String = "", val track: String = "")
object FirestoreUtils {
private val firestoreInstance: FirebaseFirestore by lazy {
FirebaseFirestore.getInstance()
}
private val songCollection = firestoreInstance.collection(SONG_COLLECTION)
private val song = mutableListOf<SongTrack>()
val songList: MutableLiveData<MutableList<SongTrack>> = MutableLiveData()
fun getSongs(): LiveData<MutableList<SongTrack>> {
firestoreInstance.collection(SONG_COLLECTION)
.addSnapshotListener { querySnapshot,
firebaseFirestoreException ->
song.clear()
querySnapshot?.documents?.forEach {
it.toObject<SongTrack>()?.let { songTrack ->
song.add(songTrack)
}
}
}
songList.postValue(song)
return songList
}
fun addSong(songTrack: SongTrack, onComplete: (String) -> Unit) {
songCollection.document(songTrack.name)
.set(songTrack)
.addOnSuccessListener { onComplete("Data Successfully added") }
.addOnFailureListener { onComplete("Data unSuccessfully added : ${it.message}") }
}
fun updateSong(songTrack: SongTrack, onComplete: (String) -> Unit) {
songCollection.document(songTrack.name)
.set(songTrack)
.addOnSuccessListener { onComplete("Data Successfully updated") }
.addOnFailureListener { onComplete("Data unSuccessfully updated : ${it.message}") }
}
fun deleteSong(songTrack: SongTrack, onComplete: (String) -> Unit) {
val deleteSong = songTrack.name
songCollection.document(songTrack.name).delete()
.addOnSuccessListener { onComplete("Song $deleteSong deleted") }
.addOnFailureListener { onComplete("Not found the song id : ${songTrack.name}") }
}
}
I just edit from your code, Did not test yet!!!
Enjoy.