AndroidViewModel | Separating data and state handling | Android library
kandi X-RAY | AndroidViewModel Summary
kandi X-RAY | AndroidViewModel Summary
Data binding is supported - extend [ViewModelBaseBindingFragment.java] library/src/main/java/eu/inloop/viewmodel/binding/ViewModelBaseBindingFragment.java) instead of ViewModelBaseFragment and implement ``getViewModelBindingConfig()`` in your Fragment. That’s it. You can then directly use ObservableField in your ViewModels. See [example] sample/src/main/java/eu/inloop/viewmodel/sample/viewmodel/SampleBindingViewModel.java). Special handling for FragmentStatePagerAdapter. A unique global ID is generated for the first time your Fragment or Activity is shown. This ID is passed on during orientation changes. Opening another instance of the same Fragment or Activity will result in a different ID. The ID is unique screen identifier. A ViewModel class is created and bound to this ID. The corresponding ViewModel instance is attached to your Fragment or Activity after an orientation change or if you return to the fragment in the back stack. The ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- When creating a ViewModel
- Returns the view model with the given identifier
- Called when the screen is created
- Get the generic type of a given class
- Gets the list of users
- Load the users
- On createView
- Perform the actual binding
- Creates the view which is used to create the fragment
- Create a new SampleBundleFragment with the given userId
- Registers the view when the view is created
- Delete a user
- Shows the list of users
- Create a new PagerFragment instance
- Creates a user list view
- Creates activity binding fragment
- Set up the activity s instance
- Destroys the fragment at the given position
AndroidViewModel Key Features
AndroidViewModel Examples and Code Snippets
Community Discussions
Trending Discussions on AndroidViewModel
QUESTION
Got a error when I try to launch the app with the emulator. Try to look up the AlbumListFragment errors at line: 40 and 41. I can't understand what I have done wrong. Have implemented this code severals times without errors.
I also use android:text="@{viewModel.response}" inside the fragment layout. I don't think databinding is the problem.
Anyone see what I do wrong?
...ANSWER
Answered 2022-Apr-03 at 16:59The real error is the one listed lower down
QUESTION
I want to use a ViewModel
with a Databinding to disable a Checkbox when a Button is clicked, but the UI won't update unless the fragment is destroyed and recreated.
There seem to be many similar questions, but the ones that are most similar seem to all be solved by setting binding.lifecycleOwner
, which I've already done.
fragment_checkbox_databinding.xml:
...ANSWER
Answered 2021-Oct-08 at 01:22var checkboxEnabled = true
The top statement creates a non-observable boolean, so no changes on that variable can be observed and submitted to the layout through the life cycle owner (which in your case is the fragment).
Sets the LifecycleOwner that should be used for observing changes of LiveData in this binding. If a LiveData is in one of the binding expressions and no LifecycleOwner is set, the LiveData will not be observed and updates to it will not be propagated to the UI.
So, using binding.lifecycleOwner = viewLifecycleOwner
alone is not enough to publish the changes to the layout through databinding, but also you've to use observable objects that can be observed by the lifeCycleOwner and eventually publish any changes on them to the layout via the DataBinding.
To solve this you need to change the checkboxEnabled
to an observable:
Option 1: use ObservableBoolean:
QUESTION
Hey I am working in kotlin flow in android. I noticed that my kotlin flow collectLatest
is calling twice and sometimes even more. I tried this answer but it didn't work for me. I printed the log inside my collectLatest
function it print the log. I am adding the code
MainActivity.kt
...ANSWER
Answered 2022-Jan-18 at 09:45The reason is collectLatest like backpressure. If you pass multiple items at once, flow will collect latest only, but if there are some time between emits, flow will collect each like latest
EDITED: You really need read about MVVM architecture.
QUESTION
I am trying to get Formatting String in AndroidViewModel
by using following extension function but failed:
ANSWER
Answered 2022-Mar-29 at 01:49vararg
parameters are Array
types. Use the *
operator to expand them:
QUESTION
My List doesn't seem to be getting initialised and I'm not really sure what I should be doing here, everytime I run I get:
...ANSWER
Answered 2022-Mar-01 at 16:44The following line in onCreateViewHolder()
is causing the crash:
val binding = FragmentHomeBinding.inflate(LayoutInflater.from(parent.context))
You are inflating the FragmentHomeBinding
, which has no TextView
with id R.id.tv_alarm_time. Use that of the RecyclerView
item instead.
QUESTION
Like title I can't get correct item data, now imagine I have 2 recycler view list on my main fragment, one recycler view is vertical, other horizontal. If I use only one recycler everything is working fine. But when I add other one it's opening items randomly, but that random data openings only random both recyclers positions. Like my first recycler have [a,b,c,d] , other recycler have [e,f,g,h]. When I click first item of first recycler its go to other fragment which shows detail, its opening e instead of a, if I go back and retry it, its opening e again, retry and its a !! so its opening randomly, why this is happening why can't I open correct data? I put Log.d on my adapters and when I click recycler items, adapters log says u clicked "a" but my detail fragment shows me data of "e". What I am doing wrong? Here is my codes:(I have lots of codes so I share what we need)
my databases TvDAO:
...ANSWER
Answered 2022-Feb-04 at 14:58i solved my problem but i don't know what i changed on background i just seperate my one function to two function in MovieListesiViewmodel and gave a parameter and its worked like a miracle
QUESTION
I am struggling to use Live data on an MVVM pattern. The app is supposed to:
- Fetch data from an API (which it does correctly)
- Store that data in the Live data object from the ViewModel
- Then the fragment calls the Observer method to fill the recyclerView.
The problem comes in point 3, it does nothing, and I cannot find the solution.
Here is the relevant code. (If I'm missing something, I will try to answer as quickly as possible)
Main Activity:
...ANSWER
Answered 2022-Jan-11 at 14:06I think you don't need to switch Coroutine contexts. A few changes I'd expect if I were reviewing this code:
This should all be in the same IO
context. You then postValue
to your liveData.
QUESTION
I have this fragment in which I store my 'favorite items' and I can delete them when I click on a button if I want to. The implementation works well until I get to the last item and it doesn't disappear unless I go to another fragment and then come back (as in, the item is deleted but the recycler view still shows it unless I update the fragment myself).
How can I make the last item disappear right away? Setting notifyDataSetChanged() after the deleteHandler in the adapter does not seem to work.
This is the fragment where I have the items:
...ANSWER
Answered 2021-Nov-24 at 13:29Clear your favourites list before setting the new items in it. You can do this in your setData()
function. Like this,
QUESTION
How to handle query method's return type room using Kotlin, Coroutines, ViewModel, LiveData
the building is failed and I'm getting lots of errors that are pointing into my Dao class and the error is
Error 1:
Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows).
Error 2:
error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this. kotlin.coroutines.Continuation continuation);
Error 3:
error: Unused parameter: continuation public abstract java.lang.Object clear(@org.jetbrains.annotations.NotNull()
Error 4:
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. kotlin.coroutines.Continuation continuation);
Error 5:
error: Not sure how to handle insert method's return type. public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull()
**Here is my full code: https://drive.google.com/drive/folders/1qWoud5XogzkTmpa-GWxLJStfdUPSoV7r?usp=sharing
android kotlin - Coroutines Room ViewModel LiveData MainActivity.kt
...ANSWER
Answered 2021-Nov-17 at 04:58Your problem in the keyword suspend in methods insert and delete. After remove your errors is gone.
QUESTION
I am creating an app where the user can log in or register for an account. I have created the register screen and it's successfully saving data into the database. However, I am now trying to integrate some validation. For example, the usernames would have to be unique and the email can't already exist.
I tried to write a custom query of-course to print out all the rows in the username column like this:
SELECT userName from cx_table
and I also tried to write a separate custom query of-course to print out all the rows in the email column like this:
SELECT email from cx_table
Then my approach was to take the user input and compare it to the values returned by that column, if it exists, print an error message. But when I run the app, I get the following error message
The columns returned by the query does not have the fields [id,firstName,lastName,password,address,city,postalcode,email,phone] in com.cxpro.data.Customer even though they are annotated as non-null or primitive. Columns returned by the query: [userName]
here is all my code for the Room Database:
Customer.kt
ANSWER
Answered 2021-Nov-17 at 00:18Then my approach was to take the user input and compare it to the values returned by that column, if it exists, print an error message. But when I run the app, I get the following error message
This is because there are insufficient values to build a Customer object. As you just returning a single value per row you can use List
instead of List
. If multiple values then you need an object probably a POJO. and that the object field/variable names match the columns names
However, rather then having to loop through 2 lists you may wish to consider changing the Customer Entity to :-
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install AndroidViewModel
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page