zoukankan      html  css  js  c++  java
  • 安卓基础(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显示:

     Run显示:

     

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

  • 相关阅读:
    匀速不间断旋转动画
    调用底层不能直接访问的类和方法
    当SD卡拔出时,返回首页,栈中的activity都要清除,只留下首页的activity
    Android 视频播放器切换到下个视频时残留上个视频画面的解决办法
    监听SD卡状态
    Android之SeekBar定制
    setRequestedOrientation设置屏幕方向
    【初级算法】15. 有效的字母异位词
    【初级算法】14. 字符串中的第一个唯一字符
    【初级算法】13. 颠倒整数
  • 原文地址:https://www.cnblogs.com/yangxionghao/p/12308348.html
Copyright © 2011-2022 走看看