ViewModel, репозиторий и класс Dao упомянуты ниже.
Создание экземпляра Viewmodel во фрагменте.
playlistVideoViewModel = ViewModelProviders.of(this).get(PlaylistVideoViewModel.class);
playlistVideoViewModel.getAllPlaylistVideos(String.valueOf(currentBucketId)).observe(this, new PlaylistSongObserver());
Представление Класс модели:
public class PlaylistVideoViewModel extends AndroidViewModel {
private MutableLiveData<List<PlaylistSong>> allPlaylistSongs;
private PlaylistVideoRepo playlistVideoRepo;
public PlaylistVideoViewModel(@NonNull Application application) {
super(application);
playlistVideoRepo = new PlaylistVideoRepo(application);
allPlaylistSongs = new MutableLiveData<>();
}
public LiveData<List<PlaylistSong>> getAllPlaylistVideos(String playlistId) {
new GetPlaylistSongs().execute(playlistId);
return allPlaylistSongs;
}
public void delete(String path) {
playlistVideoRepo.delete(path);
}
@SuppressLint("StaticFieldLeak")
class GetPlaylistSongs extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
allPlaylistSongs.postValue(playlistVideoRepo.getAllPlaylistSongs(strings[0]));
return null;
}
}}
Репозиторий:
class PlaylistVideoRepo {
private final PlaylistSongDao playlistSongDao;
PlaylistVideoRepo(Application application) {
AppDatabase appDatabase = AppDatabase.getDatabase(application);
playlistSongDao = appDatabase.playlistSongDao();
}
public List<PlaylistSong> getAllPlaylistSongs(String playlistId) {
return playlistSongDao.getAllPlaylistSongs(playlistId);
}
public void delete(String path) {
new DeletePlaylistVideoAsyncTask(playlistSongDao).execute(path);
}}
Dao:
@Dao
public interface PlaylistSongDao {
@Query("SELECT * FROM " + PlaylistVideoConstants.TABLE_NAME +
" WHERE " + PlaylistVideoConstants.Columns.PLAYLIST_ID + " = :playlistId")
List<PlaylistSong> getAllPlaylistSongs(String playlistId);
@Query("DELETE FROM "+ PlaylistVideoConstants.TABLE_NAME + " WHERE " + PlaylistVideoConstants.Columns.PATH + " = :path")
void delete(String path);}
Теперь проблема заключается в том, когда я собираюсь удалить любые данные.Я не получаю никакого обратного вызова от моего наблюдателя.
Пожалуйста, исправьте, если я что-то делаю неправильно.