0

I am working on a dynamic list which is viewed on RecyclerView. So far it is possible to add and remove elements. Also the user can change the content of every single element through several popUpWindows. However, MyViewHolder class got quite long due to many onClickListeners. So I carried MyRecyclerViewAdapter class in a separate file from the rest of the activity. Now,

  1. Is it a good practice to keep MyViewHolder class long with many click listeners (doing the most of the work inside the Adapter object), or should I retrieve the relevant data from MyRecyclerViewAdapter somehow and do the 'delete, add, edit text' work inside onCreate section?

  2. What are the most efficient, simple and fast solutions to show a totally new and different view when all elements are deleted? I tried VISIBLE, GONE solution but MyAdapter is in a separate file and I don't know how to communicate with the onCreate section to transfer real-time size of the dynamic list.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Xfce4
  • 557
  • 5
  • 28

2 Answers2

0

1/ if your actions are about interact with the item component (add, delete, edit, get content...) in a list your should put the function in Adapter, click on ViewHolder should only give its position in adapter. Solution here : RecyclerView itemClickListener in Kotlin

2/ why use VISIBLE, GONE ? when you delete a item that mean you delete the item in your data list so just reload the view after that, adapter will auto show the remain data

  • Used VISIBLE, GONE because when there is no element in the list I want to give feedback to the user, and show instructions about how to add a new item. – Xfce4 Feb 13 '20 at 10:11
  • Kiet Phan, I carried the clickListeners, check statements, popUps etc. from ´MyWievHolder´ class into the ´onBindViewHolder´ function (changing ´view´ to ´it´, ´adapterPosition´ to ´holder.adapterPosition´ etc). As far as I tested it on a phone the code operates as stable as before. Thanks for the tip. – Xfce4 Feb 13 '20 at 10:29
  • @ESoyaker oh, i didn't see the "all" in your original post, thought it just delete one item. In this case, the idea still same : you check if your data list is null before create Adapter (in activity/fragment) then do your logic, the recycler view still there but not showing anything until you create adapter for it – Kiet Phan Feb 13 '20 at 11:22
  • The problem is I need to change the ´visibility´ on general layout, not in the RecyclerView element layout. ´MyRecyclerViewAdapter´ is on a separate class File and I don't know how to communicate with the ´onCreate´ part to change ´visibility´ inside the current activity's layout. How can I modify the view of the current activity from another class File? What is the syntax? – Xfce4 Feb 13 '20 at 11:54
  • If i understand correctly, you ideal is when user delete all data the layout show some text (or whatever) to indicate current is no data, right ? Also you seems misunderstanding about onCreate. That method only call once in lifecycle, if you want to do something transition between layout use onResume (or onActivityResult) instead – Kiet Phan Feb 13 '20 at 13:54
  • Kiet Phan. Yes, when user deletes all data, I want to show a different layout inside the recyclerview segment (which covers the whole screen in my case). `onCreate` is called once. When all elements are deleted, am I still not in `onCreate` section? – Xfce4 Feb 13 '20 at 15:10
0

Answers:

Question 1: Kiet Phan's recommendation of carrying the logic part inside onBindViewHolder function was quite helpful.

Question 2: If you have already passed the current context to the primary constructor of the separate class (which is defined in another file), it is possible in Kotlin to reach the content of currentActivity by the code segment: (context as Activity).

So the solution and the general structure of my adapter was like this:

 import ...

 class MyAdapter (var ctx: Context, var list: ArrayList<SomeObject>)
    : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder {
    return MyViewHolder(LayoutInflater.from(ctx).inflate(R.layout.singleListElement, parent,false))
    }

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

    override fun onBindViewHolder(holder: MyAdapter.MyViewHolder, position: Int) {

    //Do the work here, onClickListeners, if statements etc.
    //...
    list.remove(index)
    if(list.size == 0) {
    (ctx as Activity).findViewById<TextView>(R.id.listEmpty).visibility=View.VISIBLE
       }
    } //Close onBindViewHolder

    inner class MyViewHolder (v: View) : RecyclerView.ViewHolder(v){

      val someView: Textview
      init { someView = v.findViewById(R.id.someViewId) }
    }  
}  

It is not easy to write code into this window. When I press Tab button, browser exits the edit screen and selects another part of this webpage. So the code looks like this. Maybe Copy/Paste would do better.

Improvements and more effective solutions for the empty list case are welcome. Thank you.

Xfce4
  • 557
  • 5
  • 28