in Education by
I am trying to understand ViewModel and LiveData. In MainActivity, I am observing LiveData In MyTask, I am setting data on the LiveData, that should be displayed in the activity. Problem is data set in MyTask is not getting updated on the UI. MainActivity public class MainActivity extends AppCompatActivity { private MyViewModel viewModel; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv2 = findViewById(R.id.textView2); viewModel = ViewModelProviders.of(this).get(MyViewModel.class); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewModel.setNameData("Button clicked"); new MyTask(getApplication()).execute(); } }); viewModel.getNameData().observe(this, new Observer() { @Override public void onChanged(@Nullable String s) { tv2.setText(s); } }); } } ViewModel class public class MyViewModel extends AndroidViewModel { private MutableLiveData nameData = new MutableLiveData<>(); public MutableLiveData getNameData() { return nameData; } public void setNameData(String name) { nameData.postValue(name); } public MyViewModel(@NonNull Application application) { super(application); } } MyTask class public class MyTask extends AsyncTask { private MyViewModel viewModel; public MyTask(Application application){ viewModel = new MyViewModel(application); } @Override protected Void doInBackground(Void... voids) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); viewModel.setNameData("Done task"); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Instead of creating a new instance just pass the created instance of viewmodel public MyTask(MyViewModel myViewModel) { viewmodel = myViewModel; } And then try to update the Ui To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..

Related questions

0 votes
    I am trying to understand ViewModel and LiveData. In MainActivity, I am observing LiveData In MyTask, I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have implemented a User Control in which I have bound the DataContext to itself like this... this. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I've got the following code in my MainPage.xaml.cs public partial class MainPage : UserControl { private ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    The __________ requires that each transaction Ti executes in two or three different phases in its lifetime ... topic in section Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? A. Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Dec 20, 2022 in Education by JackTerrance
0 votes
    When you run git fetch from your local repo, it will update your local changes in working area? A. True – ... It updates local repo only and does not affect the working area...
asked Dec 20, 2022 in Education by JackTerrance
0 votes
    Asynchronous update ensures that the next state is atmost unit hamming distance from current state, is that true? ... Networks of Neural Networks Please answer the above question....
asked Aug 30, 2022 in Education by JackTerrance
0 votes
    What is the other way to reduce error in recall due to false minima apart from stochastic update? (a) no other ... (d) none of the mentioned Please answer the above question....
asked Aug 30, 2022 in Education by JackTerrance
0 votes
    Is "Select column from updated" available in "After Update" trigger? If not what other approach can be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Is "Select column from updated" available in "After Update" trigger? If not what other approach can be ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a model and store that take in values from my form fields. My form contains a textfield, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    In SQL Server, it's possible to insert into a table using a SELECT statement: INSERT INTO Table (col1, col2, ... .id = other_table.id Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    Can you tell how in Subversion, how the command commit can be put separated from the command update?...
asked Feb 18, 2021 in Technology by JackTerrance
0 votes
    How do I update the GUI from another thread in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    When you run git fetch from my local repo, will it update your local code and target branch? (1)Yes, the command ... new commits and keep them in a separate branch in local repo...
asked Oct 26, 2020 in Technology by JackTerrance
...