I want to make a simple data entry application where the app starts with the main activity and asks the user for their info and image URI from the gallery, after getting all the inputs when the user enters submit it opens up a new activity that shows the information in a recycler view, now if the user wants to add more users there's a floating action button on the bottom of the recycler view screen which in turn I want to revisit the same page to ask user details, how can I implement this functionality in my code
What I have tried:
So far I have implemented the main activity, the details for the first user are showing correctly however on inputting details for the second user, the first user is getting overwritten.
MAIN ACTIVITY
package com.example.recyclerview_firebase
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var choose_pic: Button
private lateinit var submit: Button
private lateinit var preview_img: ImageView
private lateinit var heading: EditText
private lateinit var subhead: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
choose_pic = findViewById(R.id.add_pic)
submit = findViewById(R.id.submit)
heading = findViewById(R.id.name)
subhead = findViewById(R.id.proffesion)
preview_img = findViewById(R.id.imageView)
choose_pic.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,101)
}
}
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == 101)
{
preview_img.setImageURI(data?.data)
preview_img.visibility = View.VISIBLE
}
submit.setOnClickListener {
Log.i("Button", "Button clicked")
val intent = Intent(this,list::class.java)
val image = data?.data
val name = heading.text.toString()
val profession = subhead.text.toString()
intent.putExtra("image",image)
intent.putExtra("name", name)
intent.putExtra("profession",profession)
startActivity(intent)
}
}
}
LIST ACTIVITY
package com.example.recyclerview_firebase
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.view.Window
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class list : AppCompatActivity() {
private lateinit var recycle: RecyclerView
private lateinit var adapter: listadapter
private lateinit var factory: factorymodel
private lateinit var repo: repo
private lateinit var fab: FloatingActionButton
private lateinit var name: EditText
private lateinit var profession: EditText
private lateinit var addpic: Button
private lateinit var submit: Button
private lateinit var preview_img: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val image: Uri? = intent.getParcelableExtra("image")
val name = intent.getStringExtra("name")
val profession = intent.getStringExtra("profession")
setContentView(R.layout.recyclerview)
recycle = findViewById(R.id.rv)
repo = repo()
factory = factorymodel(repo)
val view: viewmodel by lazy {
ViewModelProvider(this,factorymodel(repo))[viewmodel::class.java]
}
view.list.observe(this){
adapter = listadapter(it)
recycle.adapter = adapter
}
recycle.layoutManager = LinearLayoutManager(this)
val contact = user(image!!,name!!,profession!!)
view.addcontact(contact)
Log.i("Button","User added via list")
fab = findViewById(R.id.fab)
fab.setOnClickListener{
showdialog()
}
}
private fun showdialog() {
val dialog = Dialog(this)
dialog.setCancelable(true)
dialog.setContentView(R.layout.activity_main)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
name = findViewById(R.id.name)
profession = findViewById(R.id.proffesion)
addpic = findViewById(R.id.add_pic)
submit = findViewById(R.id.submit)
preview_img = findViewById(R.id.imageView)
addpic.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, 101)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == 101)
{
preview_img.setImageURI(data?.data)
preview_img.visibility = View.VISIBLE
}
submit.setOnClickListener {
val intent = Intent(this,list::class.java)
val newimage = data?.data
val newname = name.text.toString()
val newprofession = profession.text.toString()
intent.putExtra("image",newimage)
intent.putExtra("name",newname)
intent.putExtra("profession",newprofession)
startActivity(intent)
}
}
}