I am trying to develop this small app to do CRUD operations using Room
, Repository
, LiveData
, and ViewModel
and Listview
, if you want to see development of the app or commit line of Java, here in this my github repository
The original app which is called Pets at original repository of Pets is developed by using ContentProvider
and ContentResolver
in the subclass of SQLiteOpenHelper
in Android with Java
the question is
Small App of android in Java to do CRUD operations by Room
, Repository
, LiveData
, and ViewModel
and Listview
, how to return the count of deleted rows from Dao
and AsyncTask
in Repository through ViewModel
back to CatalogActivity
?
This is what in PetDao.java
@Query("DELETE FROM pets")
int deleteAllPets();
This is what in 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();
}
This is what in PetViewModel.java
public void deleteAllPets()
{
this.petRepository.deleteAllPets();
}
This is what in 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();
}
I am also expecting an answer from @EpicPandaForce
Thank you all very much
this is what in PetDao.java
@Query("DELETE FROM pets")
int deleteAllPets();
In your 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;
}
In PetViewModel.java
public LiveData<Integer> deleteAllPets() {
return this.petRepository.deleteAllPets();
}
In 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.
}