自做闪图网站网站建设需要什么材料
news/
2025/9/23 22:21:27/
文章来源:
自做闪图网站,网站建设需要什么材料,蓝色企业网站,shopnc商城系统本是青灯不归客#xff0c;却因浊酒恋红尘 一#xff0c;基本使用
关于Room数据库的基本使用#xff0c;请参考文章Android--Jetpack--数据库Room详解一-CSDN博客 二#xff0c;Room与ViewModle,LiveData的结合使用
LiveData与ViewModle的使用#xff0c;请参考文章Andr…本是青灯不归客却因浊酒恋红尘 一基本使用
关于Room数据库的基本使用请参考文章Android--Jetpack--数据库Room详解一-CSDN博客 二Room与ViewModle,LiveData的结合使用
LiveData与ViewModle的使用请参考文章Android--Jetpack--LiveData-CSDN博客
我们通过结合Room与LiveData和ViewModle的使用可以使当我们的数据库发生变化的时候自动的去更新UI。
下面来看一个简单的使用案例
1还是 Android--Jetpack--数据库Room详解一-CSDN博客
中创建的数据库表还是YuanZhen这张表 我们把YuanZhenDao这个Dao类添加一个新的方法使得可以查询到LiveData包装的集合
Dao
public interface YuanZhenDao {Insertvoid insert(YuanZhen... yuanzhens);Deletevoid delete(YuanZhen yuanZhen);Updatevoid update(YuanZhen yuanZhen);Query(select * from YuanZhen)ListYuanZhen getAll();Query(select * from YuanZhen where name like :name)YuanZhen getByName(String name);Query(select * from YuanZhen where age in(:ages))ListYuanZhen getByAges(int[] ages);Query(select name,address from YuanZhen )public ListYuanZhenNew getNew();Query(select * from YuanZhen)LiveDataListYuanZhen getAllLiveDataYZ();
}
2将数据库MyDatabase修改为单例模式
Database(entities {YuanZhen.class},version 1)
public abstract class MyDatabase extends RoomDatabase {private static MyDatabase instance;public static synchronized MyDatabase getInstance(Context context){if(instancenull){instance Room.databaseBuilder(context.getApplicationContext(),MyDatabase.class,YuanZhenDb).build();}return instance;}public abstract YuanZhenDao yuanZhenDao();}
3创建一个包装类包装LiveData给ViewModel使用
public class YuanZhenDecorate {private LiveDataListYuanZhen liveDataAllYZ;private YuanZhenDao yuanZhenDao;public YuanZhenDecorate(Context context) {yuanZhenDao MyDatabase.getInstance(context).yuanZhenDao();if(liveDataAllYZnull){liveDataAllYZyuanZhenDao.getAllLiveDataYZ();}}void insert(YuanZhen... yuanZhens){yuanZhenDao.insert(yuanZhens);}void delete(YuanZhen yuanZhen){yuanZhenDao.delete(yuanZhen);}void update(YuanZhen yuanZhen){yuanZhenDao.update(yuanZhen);}ListYuanZhen getAll(){return yuanZhenDao.getAll();}LiveDataListYuanZhen getAllLiveDataYZ(){return yuanZhenDao.getAllLiveDataYZ();}}
4创建一个viewmodle
public class YZViewModdel extends AndroidViewModel {private YuanZhenDecorate yuanZhenDecorate;public YZViewModdel(NonNull Application application) {super(application);yuanZhenDecorate new YuanZhenDecorate(application);}void insert(YuanZhen... yuanZhens){yuanZhenDecorate.insert(yuanZhens);}void delete(YuanZhen yuanZhen){yuanZhenDecorate.delete(yuanZhen);}void update(YuanZhen yuanZhen){yuanZhenDecorate.update(yuanZhen);}ListYuanZhen getAll(){return yuanZhenDecorate.getAll();}LiveDataListYuanZhen getAllLiveDataYZ(){return yuanZhenDecorate.getAllLiveDataYZ();}}
5创建一个recyclerview的adapter
public class MyAdapter extends RecyclerView.Adapter {private LayoutInflater mLayoutInflater;private ListYuanZhen mList;public MyAdapter(Context context,ListYuanZhen mList) {mLayoutInflater LayoutInflater.from(context);this.mList mList;}public void setData(ListYuanZhen mList) {this.mList mList;}NonNullOverridepublic RecyclerView.ViewHolder onCreateViewHolder(NonNull ViewGroup parent, int viewType) {return new ViewHolder(mLayoutInflater.inflate(R.layout.item, parent, false));}Overridepublic void onBindViewHolder(NonNull RecyclerView.ViewHolder holder, int position) {((ViewHolder)holder).mTxt.setText(mList.get(position).getName());}Overridepublic int getItemCount() {if(mList!null){return mList.size();}return 0;}class ViewHolder extends RecyclerView.ViewHolder {TextView mTxt;public ViewHolder(NonNull View itemView) {super(itemView);mTxt (TextView) itemView.findViewById(R.id.txt);}}} 6activity的xml布局
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivityandroidx.recyclerview.widget.RecyclerViewandroid:idid/rv_roomandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentapp:layout_constraintBottom_toBottomOfparentapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent //androidx.constraintlayout.widget.ConstraintLayout
7item的xml布局
?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentTextViewandroid:idid/txtandroid:layout_widthmatch_parentandroid:layout_height30dpandroid:textSize16sp//RelativeLayout 8使用
public class MainActivity extends AppCompatActivity {StudentViewModel studentViewModel;ListView listView;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView findViewById(R.id.listView);studentViewModel ViewModelProviders.of(this).get(StudentViewModel.class);studentViewModel.getAllLiveDataStudent().observe(this, new ObserverListStudent() {Overridepublic void onChanged(ListStudent students) {listView.setAdapter(new GoodsAdapter(MainActivity.this, students));}});for (int i 0; i 50; i) {studentViewModel.insert(new Student(jett, 123, 1));}new Thread() {Overridepublic void run() {for (int i 0; i 50; i) {try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}studentViewModel.update(new Student(6, jett i, 123, 1));}}}.start();}
} 9运行 三数据库的升级
1强制升级执行之后数据库的结构会发生变化但是数据库的数据会丢失。
这种情况比较适合toB开发数据库版本高降到低的情况紧急发一版新的程序给现场升级。
使用 fallbackToDestructiveMigration()
Database(entities {YuanZhen.class},version 2)
public abstract class MyDatabase extends RoomDatabase {private static MyDatabase instance;public static synchronized MyDatabase getInstance(Context context){if(instancenull){instance Room.databaseBuilder(context.getApplicationContext(),MyDatabase.class,YuanZhenDb)//强制升级.fallbackToDestructiveMigration().build();}return instance;}public abstract YuanZhenDao yuanZhenDao();} 2一般的升级方式
假如我们要增加一个字段price
Entity
public class YuanZhen {PrimaryKey(autoGenerate true)private int id;ColumnInfo(name name)private String name;ColumnInfo(name age)private int age;ColumnInfo(name address)private String address;ColumnInfo(name price)private int price;Ignoreprivate String sex;public YuanZhen(String name, int age, String address) {this.name name;this.age age;this.address address;}public void setId(int id) {this.id id;}public void setName(String name) {this.name name;}public void setAge(int age) {this.age age;}public void setAddress(String address) {this.address address;}public int getId() {return id;}public String getName() {return name;}public int getAge() {return age;}public String getAddress() {return address;}Overridepublic String toString() {return YuanZhen{ id id , name name \ , age age , address address \ , sex sex \ };}
}
在MyDatabase中增加升级功能
Database(entities {YuanZhen.class},version 2,exportSchema false)
public abstract class MyDatabase extends RoomDatabase {private static MyDatabase instance;public static synchronized MyDatabase getInstance(Context context){if(instancenull){instance Room.databaseBuilder(context.getApplicationContext(),MyDatabase.class,YuanZhenDb)//强制升级// .fallbackToDestructiveMigration().addMigrations(MIGRATION_1_2).build();}return instance;}public abstract YuanZhenDao yuanZhenDao();static final Migration MIGRATION_1_2new Migration(1,2) {Overridepublic void migrate(NonNull SupportSQLiteDatabase database) {//在这里用sql脚本完成数据变化database.execSQL(alter table yuanzhen add column price integer not null default 1);}};}
这里和greendao最大的不同就是这里需要自己去写升级脚本虽然增加了工作量但是也更加灵活了。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/914071.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!