Executing tasks: [:app:assembleDebug] in project C:\Users\user\Desktop\ViewModelExample-master
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:checkDebugManifest UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:prepareLintJar UP-TO-DATE
:app:mainApkListPersistenceDebug UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:createDebugCompatibleScreenManifests UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:splitsDiscoveryTaskDebug UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:javaPreCompileDebug UP-TO-DATE
:app:compileDebugJavaWithJavacC:\Users\user\Desktop\ViewModelExample-master\app\src\main\java\project\dajver\com\viewmodelexample\MainActivity.java:38: error: data has private access in ScoreViewModel
model.data.observe(this, (s) -> {
^
1 error
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
15 actionable tasks: 1 executed, 14 up-to-date
package project.dajver.com.viewmodelexample;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
TextView textView;
private ScoreViewModel mViewModel;
private int counterA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
textView = (TextView)findViewById(R.id.textView);
ScoreViewModel model = ViewModelProviders.of(this).get(ScoreViewModel.class);
model.loadData();
model.data.observe(this, (s) -> {
textView.setText(s);
});
}
}
package project.dajver.com.viewmodelexample;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.os.AsyncTask;
import java.util.Random;
public class ScoreViewModel extends ViewModel {
private MutableLiveData<String> data = new MutableLiveData<>();
private MutableLiveData<Boolean> Loading = new MutableLiveData<>();
public void loadData() {
if (data.getValue() == null && Loading.getValue() == null) {
new DataLoader().execute();
}
}
private class DataLoader extends AsyncTask<Void, Void, String> {
private String[] results;
private Random random;
public DataLoader() {
results = new String[]{
"Success",
"Failture",
"BigData",
};
random = new Random();
}
protected void onPreExecute(){
super.onPreExecute();
Loading.setValue(true);
}
@Override
protected String doInBackground(Void... voids) {
return null;
}
protected void onPostExecute(String s){
super.onPostExecute(s);
Loading.setValue(false);
data.setValue(s);
}
}
}