<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>viewmodel Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/viewmodel/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/viewmodel/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Thu, 10 Feb 2022 11:41:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>How to use ViewModel in Android</title>
		<link>https://creatronix.de/how-to-use-viewmodel/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Wed, 17 Nov 2021 14:35:43 +0000</pubDate>
				<category><![CDATA[Android & iOS development]]></category>
		<category><![CDATA[activityViewModels]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[livedata]]></category>
		<category><![CDATA[observer]]></category>
		<category><![CDATA[viewmodel]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=3871</guid>

					<description><![CDATA[<p>Dependencies dependencies { implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' ViewModel class and LiveData class PracticeProgressViewModel : ViewModel() { private val _text = MutableLiveData&#60;String&#62;().apply { value = "" } val text: MutableLiveData&#60;String&#62; = _text fun setText(value: String){ _text.value = value } } Fragment class PracticeProgressFragment : Fragment() { private val homeViewModel: PracticeProgressViewModel by activityViewModels() Observer Pattern override fun onCreateView( inflater:&#8230;</p>
<p>The post <a href="https://creatronix.de/how-to-use-viewmodel/">How to use ViewModel in Android</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Dependencies</h2>
<pre>dependencies {
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
</pre>
<h2>ViewModel class and LiveData</h2>
<p><span id="more-3871"></span></p>
<pre>class PracticeProgressViewModel : ViewModel() {
    
    private val _text = MutableLiveData&lt;String&gt;().apply {
        value = ""
    }

    val text: MutableLiveData&lt;String&gt; = _text

    fun setText(value: String){
        _text.value = value
    }
}</pre>
<h2>Fragment</h2>
<pre>class PracticeProgressFragment : Fragment() {

    private val <strong>homeViewModel</strong>: PracticeProgressViewModel by activityViewModels()
</pre>
<p>Observer Pattern</p>
<pre>    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        <strong>homeViewModel</strong>.text.observe(viewLifecycleOwner, {
            textView.text = it
        })
</pre>
<p>Update the ViewModel</p>
<pre>    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val button = binding.button
        button.setOnClickListener(View.OnClickListener {
        
            val totalAmoint = practiceDao.getTotalAmount()
            <strong>homeViewModel</strong>.setText(totalAmoint.toString())
        })
    }

</pre>
<p>The post <a href="https://creatronix.de/how-to-use-viewmodel/">How to use ViewModel in Android</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
