gpt4 book ai didi

java - Java android中Room的CRUD,如何通过viewmodel将Repository中Dao和AsyncTask的删除行数返回给Catalog Activity?

转载 作者:行者123 更新时间:2023-11-30 10:02:32 27 4
gpt4 key购买 nike

我正在尝试开发这个小应用程序来使用 Room 进行 CRUD 操作, Repository , LiveData , 和 ViewModelListview ,如果您想查看应用程序的开发或 Java 的提交行,请在此处 my github repository

original repository of Pets 上称为 Pets 的原始应用程序使用 ContentProvider 开发和 ContentResolverSQLiteOpenHelper 的子类中在 Android 中使用 Java


问题是

Java 中的android 小应用程序做CRUD 操作作者Room , Repository , LiveData , 和 ViewModelListview , 如何从 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.
}

关于java - Java android中Room的CRUD,如何通过viewmodel将Repository中Dao和AsyncTask的删除行数返回给Catalog Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56872730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com