Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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)
        }
    }
}
Posted
Comments
[no name] 11-Feb-24 15:29pm    
You use the "same page" in a different context; you don't "revisit" the original context.
David Crow 13-Feb-24 9:23am    
I suggest you make list the main activity, and when the FAB is clicked, call startActivityForResult() with the "data entry" activity. When that activity returns, add the user-entered data to the adapter and refresh it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900