一、说在前面
昨天 | 完成了对记账本的账单的增删 |
今天 |
完善昨天的框架结构( 1、引入ViewModel管理数据、使MainActive 只管理界面、不再管数据了 2、引入AsyncTask、后台执行。不必强制在主线程执行 3、将Database.java修改为singleton模式、只维护一个Database 4、引入LiveData ) |
问题 | 没有 |
二、结构完善
1、引入LiveData,管理所有账单,设置观察者,自主更新界面数据,不必再调用unDataView()来更新界面。
1)、Dao
@Query("SELECT * From ACCOUNTRECORD ORDER BY ID DESC")
LiveData<List<AccountRecord>> getAllAccountRecordLive ();
2)、在MainActive中设置观察者
accountRecordViewModel.getAllAcountRecord().observe(this, new Observer<List<AccountRecord>>() { @Override public void onChanged(List<AccountRecord> accountRecords) { StringBuilder text = new StringBuilder(); for (int i=0;i<accountRecords.size();i++){ AccountRecord accountRecord = accountRecords.get(i); text.append(accountRecord.getId()).append(" ").append(accountRecord.getCostType()).append(" ").append(accountRecord.getCostTime()).append(" ").append(accountRecord.getCostMoney()).append(" "); } textView.setText(text.toString()); } });
2、引入ViewModel管理数据、使MainActive 只管理界面、不再管数据了
1)、仓库类操作数据库
package com.me.familybookkeepingbook; import android.content.Context; import android.os.AsyncTask; import androidx.lifecycle.LiveData; import java.util.List; public class AccountRecordRepository { private LiveData<List<AccountRecord>> allAcountRecord; private AccountRecordDao accountRecordDao; public AccountRecordRepository(Context context) { AccountRecordDatabase accountRecordDatabase = AccountRecordDatabase.getAccountRecordDataBase(context.getApplicationContext()); accountRecordDao = accountRecordDatabase.getAccountRecordDao(); allAcountRecord = accountRecordDao.getAllAccountRecordLive(); } public LiveData<List<AccountRecord>> getAllAcountRecord() { return allAcountRecord; } void insertAccountRecord(AccountRecord ... accountRecords){ new InsertAsyncTask(accountRecordDao).execute(accountRecords); } void deleteAllAccountRecord(){ new DeleteAsyncTask(accountRecordDao).execute(); } static class InsertAsyncTask extends AsyncTask<AccountRecord ,Void,Void> { private AccountRecordDao accountRecordDao; public InsertAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(AccountRecord... accountRecords) { accountRecordDao.insertAccountRecord(accountRecords); return null; } } static class DeleteAsyncTask extends AsyncTask<Void ,Void,Void>{ private AccountRecordDao accountRecordDao; public DeleteAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(Void... Voids) { accountRecordDao.deleteAllAccountRecord(); return null; } } }
2)、ViewModel管理数据
package com.me.familybookkeepingbook; import android.app.Application; import android.os.AsyncTask; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import java.util.List; public class AccountRecordViewModel extends AndroidViewModel { private AccountRecordRepository accountRecordRepository; public AccountRecordViewModel(@NonNull Application application) { super(application); accountRecordRepository = new AccountRecordRepository(application); } public LiveData<List<AccountRecord>> getAllAcountRecord() { return accountRecordRepository.getAllAcountRecord(); } void insertAccountRecord(AccountRecord ... accountRecords){ accountRecordRepository.insertAccountRecord(accountRecords); } void deleteAllAccountRecord(){ accountRecordRepository.deleteAllAccountRecord(); } static class InsertAsyncTask extends AsyncTask<AccountRecord ,Void,Void> { private AccountRecordDao accountRecordDao; public InsertAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(AccountRecord... accountRecords) { accountRecordDao.insertAccountRecord(accountRecords); return null; } } static class DeleteAsyncTask extends AsyncTask<Void ,Void,Void>{ private AccountRecordDao accountRecordDao; public DeleteAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(Void... Voids) { accountRecordDao.deleteAllAccountRecord(); return null; } } }
3)、MainActive 管理界面
package com.me.familybookkeepingbook; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProviders; import java.util.List; public class MainActivity extends AppCompatActivity { AccountRecordViewModel accountRecordViewModel; Button buttonInsert,buttonDelete; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); accountRecordViewModel = ViewModelProviders.of(this).get(AccountRecordViewModel.class); textView = findViewById(R.id.textView); buttonDelete = findViewById(R.id.buttonDelete); buttonInsert = findViewById(R.id.buttonInsert); accountRecordViewModel.getAllAcountRecord().observe(this, new Observer<List<AccountRecord>>() { @Override public void onChanged(List<AccountRecord> accountRecords) { StringBuilder text = new StringBuilder(); for (int i=0;i<accountRecords.size();i++){ AccountRecord accountRecord = accountRecords.get(i); text.append(accountRecord.getId()).append(" ").append(accountRecord.getCostType()).append(" ").append(accountRecord.getCostTime()).append(" ").append(accountRecord.getCostMoney()).append(" "); } textView.setText(text.toString()); } }); buttonInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AccountRecord accountRecord = new AccountRecord(1,"2020-01-26","学习",100.0); accountRecordViewModel.insertAccountRecord(accountRecord); } }); buttonDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { accountRecordViewModel.deleteAllAccountRecord(); } }); } }
3、引入AsyncTask、后台执行。不必强制在主线程执行
static class InsertAsyncTask extends AsyncTask<AccountRecord ,Void,Void> { private AccountRecordDao accountRecordDao; public InsertAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(AccountRecord... accountRecords) { accountRecordDao.insertAccountRecord(accountRecords); return null; } } static class DeleteAsyncTask extends AsyncTask<Void ,Void,Void>{ private AccountRecordDao accountRecordDao; public DeleteAsyncTask(AccountRecordDao accountRecordDao) { this.accountRecordDao = accountRecordDao; } @Override protected Void doInBackground(Void... Voids) { accountRecordDao.deleteAllAccountRecord(); return null; } }
4、将Database.java修改为singleton模式、只维护一个Database
package com.me.familybookkeepingbook; import android.content.Context; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; @Database(entities = {AccountRecord.class },version = 1,exportSchema = false) public abstract class AccountRecordDatabase extends RoomDatabase { private static AccountRecordDatabase INSTANCE; public abstract AccountRecordDao getAccountRecordDao(); static synchronized AccountRecordDatabase getAccountRecordDataBase(Context context){ if (INSTANCE == null){ INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AccountRecordDatabase.class,"accountrecord_database") .build(); } return INSTANCE; } }