Javaandroid中Room的增删改查,如何通过viewmodel将Repository中Dao和AsyncTask删除的行数return返回到Catalog Activity?
CRUD of Room in Java android, how to return the count of deleted rows from Dao and AsyncTask in Ripository through viewmodel back to CatalogActivity?
我正在尝试使用 Room
、Repository
、LiveData
、ViewModel
和 Listview
开发这个小应用程序来执行 CRUD 操作,如果您想在 my github repository
中查看 Java 的应用程序开发或提交行
最初在 original repository of Pets 上称为 Pets 的应用程序是使用 Android 中 SQLiteOpenHelper
的子类中的 ContentProvider
和 ContentResolver
开发的 Java
问题是
android 在 Java 中的小应用程序通过 Room
、Repository
、LiveData
和 ViewModel
和 Listview
,如何 return 从存储库 Dao
和 AsyncTask
中删除的行数通过 ViewModel
回到 CatalogActivity
?
这是 PetDao.java
中的内容
@Query("DELETE FROM pets")
int deleteAllPets();
这是 PetRepository.java
中的内容
// this class is inside repository
private static class DeleteAllPetsAsyncTask extends AsyncTask<Void, Void, Integer>
{
private PetDao petDaoOfDeleteAllAsyncTask;
DeleteAllPetsAsyncTask(PetDao petDao)
{
this.petDaoOfDeleteAllAsyncTask = petDao;
}
@Override
protected Integer doInBackground(Void... voids)
{
int countOfDeletedRows = this.petDaoOfDeleteAllAsyncTask.deleteAllPets();
return countOfDeletedRows;
}
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param integer The result of the operation computed by {@link #doInBackground}.
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@Override
protected void onPostExecute(Integer integer) {
//super.onPostExecute(integer);
// TODO: how to return this integer
}
}
// this function is inside repository
public void deleteAllPets()
{
new DeleteAllPetsAsyncTask(this.petDao).execute();
}
这是 PetViewModel.java
中的内容
public void deleteAllPets()
{
this.petRepository.deleteAllPets();
}
这就是 CatalogActivity.java
中的内容
private void deleteAllPets() {
// TODO: Implement this method
//
Log.v(this.LOG_TAG, "rows deleted from pet database count is: ");
this.petViewModel.deleteAllPets();
// Show a toast message depending on whether or not the delete was successful.
if (0 == 0) {
// If no rows were deleted, then there was an error with the delete.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_failed) +
" ", Toast.LENGTH_LONG).show();
} else {
// Otherwise, the delete was successful and we can display a toast.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_successful) +
" ", Toast.LENGTH_LONG).show();
}
// Close the activity
//super.finish();
}
我也期待@EpicPandaForce 的回答
非常感谢大家
这是 PetDao.java
中的内容
@Query("DELETE FROM pets")
int deleteAllPets();
在你的PetRepository.java
private static class DeleteAllPetsAsyncTask extends AsyncTask<Void, Void, Integer>
{
private PetDao petDaoOfDeleteAllAsyncTask;
public MutableLiveData<Integer> resultLiveData = new MutableLiveData();
DeleteAllPetsAsyncTask(PetDao petDao)
{
this.petDaoOfDeleteAllAsyncTask = petDao;
}
@Override
protected Integer doInBackground(Void... voids)
{
int countOfDeletedRows = this.petDaoOfDeleteAllAsyncTask.deleteAllPets();
return countOfDeletedRows;
}
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param integer The result of the operation computed by {@link #doInBackground}.
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@Override
protected void onPostExecute(Integer integer) {
//super.onPostExecute(integer);
// TODO: how to return this integer
resultLiveData.postValue(integer);
}
}
// this function is inside repository
public LiveData<Integer> deleteAllPets()
{
DeleteAllPetsAsyncTask task = new DeleteAllPetsAsyncTask(this.petDao);
task.execute();
// I edited here
return task.resultLiveData;
}
在PetViewModel.java
public LiveData<Integer> deleteAllPets() {
return this.petRepository.deleteAllPets();
}
在CatalogActivity.java
//
private void deleteAllPets() {
// TODO: Implement this method
//
Log.v(this.LOG_TAG, "rows deleted from pet database count is: ");
this.petViewModel.deleteAllPets().observe(this,new Observer<Integer>(){
@Override
public void onChanged(final Integer result) {
// here you will get result
if (result == 0) {
// If no rows were deleted, then there was an error with the delete.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_failed) +
" ", Toast.LENGTH_LONG).show();
} else {
// Otherwise, the delete was successful and we can display a toast.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_successful) +
" ", Toast.LENGTH_LONG).show();
}
// Close the activity
//super.finish();
}
});
// Show a toast message depending on whether or not the delete was successful.
}
我正在尝试使用 Room
、Repository
、LiveData
、ViewModel
和 Listview
开发这个小应用程序来执行 CRUD 操作,如果您想在 my github repository
最初在 original repository of Pets 上称为 Pets 的应用程序是使用 Android 中 SQLiteOpenHelper
的子类中的 ContentProvider
和 ContentResolver
开发的 Java
问题是
android 在 Java 中的小应用程序通过 Room
、Repository
、LiveData
和 ViewModel
和 Listview
,如何 return 从存储库 Dao
和 AsyncTask
中删除的行数通过 ViewModel
回到 CatalogActivity
?
这是 PetDao.java
@Query("DELETE FROM pets")
int deleteAllPets();
这是 PetRepository.java
// this class is inside repository
private static class DeleteAllPetsAsyncTask extends AsyncTask<Void, Void, Integer>
{
private PetDao petDaoOfDeleteAllAsyncTask;
DeleteAllPetsAsyncTask(PetDao petDao)
{
this.petDaoOfDeleteAllAsyncTask = petDao;
}
@Override
protected Integer doInBackground(Void... voids)
{
int countOfDeletedRows = this.petDaoOfDeleteAllAsyncTask.deleteAllPets();
return countOfDeletedRows;
}
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param integer The result of the operation computed by {@link #doInBackground}.
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@Override
protected void onPostExecute(Integer integer) {
//super.onPostExecute(integer);
// TODO: how to return this integer
}
}
// this function is inside repository
public void deleteAllPets()
{
new DeleteAllPetsAsyncTask(this.petDao).execute();
}
这是 PetViewModel.java
public void deleteAllPets()
{
this.petRepository.deleteAllPets();
}
这就是 CatalogActivity.java
private void deleteAllPets() {
// TODO: Implement this method
//
Log.v(this.LOG_TAG, "rows deleted from pet database count is: ");
this.petViewModel.deleteAllPets();
// Show a toast message depending on whether or not the delete was successful.
if (0 == 0) {
// If no rows were deleted, then there was an error with the delete.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_failed) +
" ", Toast.LENGTH_LONG).show();
} else {
// Otherwise, the delete was successful and we can display a toast.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_successful) +
" ", Toast.LENGTH_LONG).show();
}
// Close the activity
//super.finish();
}
我也期待@EpicPandaForce 的回答
非常感谢大家
这是 PetDao.java
中的内容@Query("DELETE FROM pets")
int deleteAllPets();
在你的PetRepository.java
private static class DeleteAllPetsAsyncTask extends AsyncTask<Void, Void, Integer>
{
private PetDao petDaoOfDeleteAllAsyncTask;
public MutableLiveData<Integer> resultLiveData = new MutableLiveData();
DeleteAllPetsAsyncTask(PetDao petDao)
{
this.petDaoOfDeleteAllAsyncTask = petDao;
}
@Override
protected Integer doInBackground(Void... voids)
{
int countOfDeletedRows = this.petDaoOfDeleteAllAsyncTask.deleteAllPets();
return countOfDeletedRows;
}
/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param integer The result of the operation computed by {@link #doInBackground}.
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@Override
protected void onPostExecute(Integer integer) {
//super.onPostExecute(integer);
// TODO: how to return this integer
resultLiveData.postValue(integer);
}
}
// this function is inside repository
public LiveData<Integer> deleteAllPets()
{
DeleteAllPetsAsyncTask task = new DeleteAllPetsAsyncTask(this.petDao);
task.execute();
// I edited here
return task.resultLiveData;
}
在PetViewModel.java
public LiveData<Integer> deleteAllPets() {
return this.petRepository.deleteAllPets();
}
在CatalogActivity.java
//
private void deleteAllPets() {
// TODO: Implement this method
//
Log.v(this.LOG_TAG, "rows deleted from pet database count is: ");
this.petViewModel.deleteAllPets().observe(this,new Observer<Integer>(){
@Override
public void onChanged(final Integer result) {
// here you will get result
if (result == 0) {
// If no rows were deleted, then there was an error with the delete.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_failed) +
" ", Toast.LENGTH_LONG).show();
} else {
// Otherwise, the delete was successful and we can display a toast.
Toast.makeText(this, super.getString(R.string.catalog_delete_all_pets_successful) +
" ", Toast.LENGTH_LONG).show();
}
// Close the activity
//super.finish();
}
});
// Show a toast message depending on whether or not the delete was successful.
}