1

I have a view with 4 containers (LinearLayouts). Each container will be filled with content depending on data. (Each content view is inflated with a layout and added to the container)

Previously I called ButterKnife.bind(this, view) after I inflated each of the layouts, when the data was loaded. Now I want to get rid of ButterKnife in favor of the kotlinx synthetic view binding.

My problem is, the first time the view is created and setup everything works fine. When I refresh the view, most of the views are null (which weren't null before and shouldn't be).

Some code:

private fun refresh() {
    //Get data...then...
    //Reset containers
    topup_container_one.removeAllViews()
    topup_container_two.removeAllViews()
    topup_container_three.removeAllViews()
    topup_container_four.removeAllViews()

    lateinit var contentOne: View
    lateinit var contentTwo: View
    lateinit var contentThree: View
    lateinit var contentFour: View
    val layoutInflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

    if (someCondition) {
        contentOne = layoutInflater.inflate(R.layout.layout1, topup_container_one, false)
        contentTwo = layoutInflater.inflate(R.layout.layout2, topup_container_two, false)
        contentThree = layoutInflater.inflate(R.layout.layout3, topup_container_three, false)
        contentFour = layoutInflater.inflate(R.layout.layout4, topup_container_four, false)       
    } else { 
         // some other sort order, same principle
    }

    topup_container_one.addView(contentOne)
    topup_container_two.addView(contentTwo)
    topup_container_three.addView(contentThree)
    topup_container_four.addView(contentFour)
} 

Layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="vertical">
<LinearLayout
    android:id="@+id/topup_container_one"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    </LinearLayout>

   //same for two, three, four

</LinearLayout>

Is there anything I can do about that view mess? Maybe 'recall' the view binding?

chrjs
  • 2,375
  • 1
  • 25
  • 41

1 Answers1

0

When I bind my views again manually after inflating each container, it works.

So, what I do:

private var buttonVoucherVoiceInput: AppCompatImageButton? = null
private var editTextVoucher: EditText? = null
private var textInputLayoutVoucher: TextInputLayout? = null
private var buttonVoucherSubmit: Button? = null
//And many many more

private fun refresh() {

    ///...
    topup_container_one.addView(contentOne)
    topup_container_two.addView(contentTwo)
    topup_container_three.addView(contentThree)
    topup_container_four.addView(contentFour)

    //Find views again
    editTextVoucher = view!!.findViewById(R.id.topup_edittext_voucher)
    buttonVoucherSubmit = view!!.findViewById(R.id.topup_button_voucher_submit)
    textInputLayoutVoucher = view!!.findViewById(R.id.topup_til_voucher)
    buttonVoucherVoiceInput = view!!.findViewById(R.id.topup_button_voucher_voice)

}

Annoying, but it works again.. And I can remove Butterknife from my project. (No disrespect, I loved it) :)

chrjs
  • 2,375
  • 1
  • 25
  • 41