'How do you pass user input into a recycler view on a different screen using a data class?

At the moment I am hardcoding some data in the id fields to check does the recycler work and if the adapter is working which it seems to be? My next step is to get the user to input data in one fragment and when the user goes back to home the recycler view should update? In the create a post fragment i have the user passing in data, and the home fragment contains the recycler view. I assume i need to update the recyclerView in the onBindViewHolder and change the getItemCount to post.size and use and ArrayList Below are my relevant classes

CreateAPostFragment:

import ie.wit.savvytutor.R
import ie.wit.savvytutor.models.PostModel
import ie.wit.savvytutor.R.layout.createapost_fragment

class CreateAPostFragment : Fragment() {

    var post = PostModel()

    override fun onCreateView(
        inflater: LayoutInflater,
        @Nullable container: ViewGroup?,
        @Nullable savedInstanceState: Bundle?
    ): View {
        //inflate the fragment layout

        val root = inflater.inflate(createapost_fragment, container, false)
        setAddButtonListener(root)
        return root

    }
    companion object {
        @JvmStatic
        fun newInstance() =
            CreateAPostFragment().apply {
                arguments = Bundle().apply {}
            }
    }


    fun setAddButtonListener(layout: View) {
        //assign the user input fields
        val addBtn = layout.findViewById<Button>(R.id.createapostbtn)
        val title = layout.findViewById<EditText>(R.id.postTitle)
        val subject =layout.findViewById<Spinner>(R.id.chooseSubject)
        val location = layout.findViewById<EditText>(R.id.chooseLocation)
        val level = layout.findViewById<Spinner>(R.id.chooseSubject)
        val description = layout.findViewById<EditText>(R.id.description)

        //take in user input and store in the model
        addBtn.setOnClickListener {

            post.title = title.text.toString()
            post.subject = subject.selectedItem.toString()
            post.location = location.text.toString()
            post.level = level.selectedItem.toString()
            post.description = description.text.toString()

        println(post)
            Toast.makeText(getActivity(), "Post Created" , Toast.LENGTH_LONG).show();
        }

    }

}

HomeFragment:

import ie.wit.savvytutor.adapters.DisplayPostAdapter
import kotlinx.android.synthetic.main.home_fragment.*

private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<DisplayPostAdapter.ViewHolder>? = null

class HomeFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        @Nullable container: ViewGroup?,
        @Nullable savedInstanceState: Bundle?
    ): View {
        //inflate the fragment layout
        val root = inflater.inflate(ie.wit.savvytutor.R.layout.home_fragment, container, false)
        return root

    }


    override fun onViewCreated(itemView: View, savedInstanceState: Bundle?) {
        super.onViewCreated(itemView, savedInstanceState)
        displayPosts.apply {
            // set a LinearLayoutManager to handle Android
            // RecyclerView behavior
            layoutManager = LinearLayoutManager(activity)
            // set the custom adapter to the RecyclerView
            adapter = DisplayPostAdapter()
        }
    }

}

DisplayPostAdapter

package ie.wit.savvytutor.adapters

import ie.wit.savvytutor.R
import ie.wit.savvytutor.fragments.HomeFragment
import ie.wit.savvytutor.models.PostModel
import org.w3c.dom.Text

class DisplayPostAdapter : RecyclerView.Adapter<DisplayPostAdapter.ViewHolder>() {

    private val title = arrayOf("this is a test", "Test 2")
    private val subject = arrayOf("Maths", "English")
    private val location = arrayOf("Wexford", "Waterford")
    private val level = arrayOf("Junior Cert", "leaving Cert")

    private val description = arrayOf("Looking for a Maths Tutor in Kilmore Quay, must be available on weekends",
    "Looking for a Enlgish Tutor in Waterford, must be available on Thursday")

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var title: TextView
        var subject: TextView
        var location: TextView
        var level: TextView
        var description: TextView

        init {

            title = itemView.findViewById(R.id.displayTitle)
            subject = itemView.findViewById(R.id.displaySubject)
            location = itemView.findViewById(R.id.displayLocation)
            level = itemView.findViewById(R.id.displayLevel)
            description = itemView.findViewById(R.id.displayDescription)

            itemView.setOnClickListener {

                val context = itemView.context
                val intent = Intent(context, HomeFragment::class.java).apply {

                    putExtra("TITLE", title.text)
                    putExtra("SUBJECT", subject.text)
                    putExtra("LOCATION", location.text)
                    putExtra("LEVEL", level.text)
                    putExtra("DESCRIPTION", description.text)

                }
                context.startActivity(intent)
            }
        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
        val v = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.displaypost_view, viewGroup, false)
        return ViewHolder(v)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
        viewHolder.title.text = title[i]
        viewHolder.subject.text = subject[i]
        viewHolder.location.text = location[i]
        viewHolder.level.text = level[i]
        viewHolder.description.text = description[i]
    }

    override fun getItemCount(): Int {
        return title.size
    }

}

PostModel:

package ie.wit.savvytutor.models

import java.io.FileDescriptor

data class PostModel(var title: String = "",
                 var subject: String = "",
                 var location: String = "",
                 var level: String = "",
                 var description: String =  ""
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source