安卓基础(AndroidViewModel)

昨天学习了AndroidViewModel,但是我根据视频上讲解,根据所讲用例,在添加依赖得时候一直报错,后来我请教大佬,他告诉我说是,因为网络得问题,国外得一些依赖有可能下不下来,所以可以下载阿里云的依赖。

implementation ‘androidx.lifecycle:lifecycle-viewmodel-savedstate:1.0.0-alpha01‘

需将build.gradle(Project)更改为以下:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ }
        maven { url ‘http://maven.aliyun.com/nexus/content/repositories/jcenter‘ }
        google()
        //jcenter()

    }
    dependencies {
        classpath ‘com.android.tools.build:gradle:3.5.3‘

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ }
        maven { url ‘http://maven.aliyun.com/nexus/content/repositories/jcenter‘ }
        google()
        //jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

跟着视频所作的项目是永久保存数据,在正常重启情况下,也可以保存数据。

项目还是一个简单的加减运算,两个按钮,一个Textview。

MainActivity:

package com.example.androidviewmodel;

import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import androidx.lifecycle.SavedStateVMFactory;

import com.example.androidviewmodel.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
MyViewModel myViewModel;
ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        myViewModel = ViewModelProviders.of(this, new SavedStateVMFactory(this)).get(MyViewModel.class);
        binding.setLifecycleOwner(this);
       binding.setData(myViewModel);
    }
}

MyViewModel:

package com.example.androidviewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateHandle;

public class MyViewModel extends AndroidViewModel {
    SavedStateHandle handle;
    String key = getApplication().getResources().getString(R.string.key);
    String mydata = getApplication().getResources().getString(R.string.MY_DATA);

    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        if (!handle.contains(key)) {
            load();
        }
    }

    public LiveData<Integer> getnumber() {
        return handle.getLiveData(key);
    }

    void load() {
        SharedPreferences shp = getApplication().getSharedPreferences(mydata, Context.MODE_PRIVATE);
        int x = shp.getInt(key, 0);
        handle.set(key, x);
    }

    void save() {
        SharedPreferences shp = getApplication().getSharedPreferences(mydata, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shp.edit();
        editor.putInt(key, getnumber().getValue());
        editor.apply();
    }

    public void add(int x) {
        handle.set(key, getnumber().getValue() + x);
    }
}

但是我遇到了一个问题,我跟着视频所敲的代码,就是报错,一直找不出为什么,很难受。

Build显示:

安卓基础(AndroidViewModel)

 Run显示:

安卓基础(AndroidViewModel)

 安卓基础(AndroidViewModel)

一直说我无法创建Myviewmodel的实例,弄了大半天,还是没结果,渴望得到大佬的指点。

相关推荐