码迷,mamicode.com
首页 > 移动开发 > 详细

安卓基础(AndroidViewModel)

时间:2020-02-14 18:50:39      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:leo   mic   conf   project   依赖   top   简单   告诉   arc   

昨天学习了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显示:

技术图片

 

 Run显示:

技术图片

 

 技术图片

 

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

 

安卓基础(AndroidViewModel)

标签:leo   mic   conf   project   依赖   top   简单   告诉   arc   

原文地址:https://www.cnblogs.com/yangxionghao/p/12308348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!