AnswerBun.com

How can I set default value for data binding in Android Studio?

Stack Overflow Asked on December 1, 2020

I have assigned the data for variable playState before fire Code A , but the Code A still get the following error information.

method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter aEPlayState

I think playState is a LiveData, the initial value is null, it maybe cause error before I assign the data to it.

How can I fix it? is Code B right?

Code A

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="android.view.View" />
        <import type="androidx.lifecycle.LiveData" />
        <import type="info.dodata.voicerecorder.model.EPlayState" />
      
        <variable
            name="playState"
            type="LiveData&lt;EPlayState>" />
      
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"      
        android:orientation="vertical">

            <ImageButton
                android:id="@+id/btnPlay"
                android:layout_width="@dimen/round_button_small"
                android:layout_height="@dimen/round_button_small"  
                app:srcCompat="@drawable/play_play"
                app:iamgeForPlayPause="@{playState}"
                
            />
     </LinearLayout>

</layout>


@BindingAdapter("app:iamgeForPlayPause")
fun imageForPlayPause(aImageButton: ImageButton, aEPlayState: EPlayState) {
    ...
}

enum class EPlayState {
    STOPPED,
    PLAYING,
    PAUSED
}

Code B

<variable
   name="playState"
   type="LiveData&lt;EPlayState>" 
   default="EPlayState.STOPPED"
/>

2 Answers

Add a null-check when you assign the value

playState != null ? .. : ..

But also reconsider why you use a livedata there..
You could also just bind the state and set it within an observer.

Correct answer by Tobi on December 1, 2020

Whether you are using MVP or MVVM architecture for your implementation. It is not good idea to have business logic manipulation on UI part of code in my opinion. The PlayState related manipulation need to be done on ViewModel or Presenter. So I suggest add this LiveData as attribute of the ViewModel/Presenter associated with your fragment, initialize it in init block of ViewModel/Presenter class. Inside your fragment, assuming name of xml for your fragment is example_layout.xml add following code inside OnCreateView function of fragment as follows :

LayoutExampleBinding.inflate(inflater, container, false).also {
            it.lifecycleOwner = viewLifecycleOwner
            it.viewModel = <ViewModel>
            it.playState = <ViewModel>.playState
}.root

(Or bind Presenter instead of ViewModel if using MVP architecture)

Answered by Anand Kamathi on December 1, 2020

Add your own answers!

Related Questions

std::map::operator[] is more efficient than std::map::insert?

1  Asked on December 27, 2020 by eddieipeace

   

Splitting a list (?)

1  Asked on December 27, 2020 by superannuated

   

Python Scrapy how to save data in different files

1  Asked on December 27, 2020 by silver-flash

   

JS get random value from array and update array

2  Asked on December 27, 2020 by nicolas-schmit

     

Caught and declared exception in Java?

1  Asked on December 26, 2020 by hrvoje-t

   

IEnumerable and Recursion using yield return

8  Asked on December 26, 2020 by jamie-dixon

       

How to parse CSV with node.js?

2  Asked on December 26, 2020 by idarosa

         

Why this program with for loop give zero when y>5 and x=2

2  Asked on December 26, 2020 by vms

 

Null pointer exception. How my connection object is pointing to null

2  Asked on December 26, 2020 by monisha-ravi

     

How do I make contents in HTML by using css

0  Asked on December 26, 2020 by jaeseo-lee

   

How to show Toaster after logout

2  Asked on December 26, 2020

   

How to write to a csv within a pandas UDF in pyspark?

0  Asked on December 26, 2020 by codemaster2020

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir