Home > database >  Is it possible to use a recyclerView inside a form?
Is it possible to use a recyclerView inside a form?

Time:09-15

I'm developing an app to store TV shows informations. The use can add shows and then view its collection. I want, when adding a show, to be able to also add seasons to it, and several if need be.

I have Show and Season models, and I've created an AddShowActivity with its add_show_activity layout. I've started using Android Studio not long ago so maybe this is not optimal, but I thought of using a RecyclerView inside of my layout, and then recycle an item_add_season layout in order to add as many seasons as I want while creating a show.

However, this has caused several problems to me, to which I couldn't find any answer and am currently lost as to what to do. I've put an Add Season button in my add_show_activity, which is supposed to add a new item_add_season to my RecyclerView, however I didn't know how I should go about doing that. And even if I still haven't tried it, I'm wondering how I'll be able to retrieve my data from outside of my Adapter.

So I've been wondering if it was possible to use a RecyclerView like that in order to add several seasons to my form ? And if not, how should I go about doing that ?

Below are my AddShowActivity and my AddSeasonAdapter (the recyclerview adapter).

class AddShowActivity : AppCompatActivity() {
    private lateinit var editTextName: EditText
    private lateinit var editTextNote: EditText
    private lateinit var confirmButton: Button
    private lateinit var addSeasonButton: Button
    private lateinit var seasonsRecyclerView: RecyclerView

    @SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_show)
        
        editTextName = findViewById(R.id.name_input)
        editTextNote = findViewById(R.id.note_input)

        seasonsRecyclerView = findViewById(R.id.seasons_recycler_view)
        seasonsRecyclerView.adapter = AddSeasonAdapter(this, 0, R.layout.item_add_season)
        seasonsRecyclerView.layoutManager = LinearLayoutManager(this)

        confirmButton = findViewById(R.id.confirm_button)
        confirmButton.setOnClickListener{
            sendForm()
        }

        addSeasonButton = findViewById(R.id.add_season_button)
        addSeasonButton.setOnClickListener {
            // Add a season to the RecyclerView and update its seasonsCount
        }
    }

    @SuppressLint("NewApi")
    private fun sendForm(){
        val repo = ShowRepository()
        val showName = editTextName.text.toString()
        val showNote = parseInt(editTextNote.text.toString())

        val seasonsList = arrayListOf<SeasonModel>() // Get info from seasons adapter and create seasons list

        val show = ShowModel(UUID.randomUUID().toString(), showName, showNote, seasonsList)
        repo.insertShow(show)
        this.finish()
    }
}
class AddSeasonAdapter(val context: AddShowActivity, private var seasonsCount: Int, private val layoutId: Int) : RecyclerView.Adapter<AddSeasonAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view){

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("NewApi")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        
    }

    override fun getItemCount(): Int = seasonsCount
}

Hope someone will be able to help me through this, thanks in advance

CodePudding user response:

I've found a YouTube video explaining exactly how to do it (this one for those who wanna see it).

So basically, the solution is not to use a RecyclerView but instead a LinearLayout in which the seasons are added when clicking on the 'Add season' button. This is quite easy to do, as the only thing to do is to inflate the layout, here my item_add_season, and then add it to the LinearLayout.

So like that:

// The LinearLayout in which items are added
val seasonsList = findViewById<LinearLayout>(R.id.seasons_list)

addSeasonButton.setOnClickListener {
        val seasonView: View = layoutInflater.inflate(R.layout.item_add_season, null, false)   

        // Initialize the seasons items components
        val seasonNumber = seasonView.findViewById<EditText>(R.id.season_number_input)
        val seasonNote = seasonView.findViewById<EditText>(R.id.season_note_input)
        val imageClose = seasonView.findViewById<ImageView>(R.id.image_close)


        imageClose.setOnClickListener {
            seasonsList.removeView(seasonView)
        }

        // Add the add_season_layout to the linearLayout
        seasonsList.addView(seasonView)
}

Hope this will be useful to someone !

  • Related