Доброе утро, у меня проблема с исходным кодом, но ошибки нет.Я создаю фрагмент с элементами папки, используя базу данных Room и viewModel.Моя проблема в том, что когда я добавляю новую папку, она не показывает приложение в folderFragment, даже если добавляет их в базу данных (т.е. без нажатия кнопки «Добавить»).(Я использовал тот же код в других действиях, и он работает, но здесь нет результата).
FolderFragment
public class AjouterDos_Fragment extends Fragment {
View view;
private DosViewModel dosViewModel;
private List<Dossier> listDos = new ArrayList<>();
public static final int ADD_DOS_REQUEST = 1;
private RecyclerView folder_recyclerView;
private FloatingActionButton btn_add_dos;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
view =
inflater.inflate(R.layout.fragment_ajouter_dos_,container,false);
btn_add_dos = view.findViewById(R.id.btn_add_dos);
btn_add_dos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomDialog();
}
});
folder_recyclerView = view.findViewById(R.id.folder_recyclerView);
folder_recyclerView.setLayoutManager(new
LinearLayoutManager(getContext()));
folder_recyclerView.setHasFixedSize(true);
// folder_recyclerView.addItemDecoration(new
DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
final DosAdapter adapter1 = new DosAdapter();
folder_recyclerView.setAdapter(adapter1);
dosViewModel = ViewModelProviders.of(this).get(DosViewModel.class);
dosViewModel.insert(new Dossier(":3333333"));
dosViewModel.getAllFolders().observe(this, new Observer<List<Dossier>>
() {
@Override
public void onChanged(@Nullable List<Dossier> dossiers) {
//update RecyclerView
adapter1.setDossiers(dossiers);
}
});
return view ;
}
private void showCustomDialog() {
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
dialog.setContentView(R.layout.fragment_add_edit_dos);
dialog.getWindow().setBackgroundDrawable(new
ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
final Button add_dos = (Button)
dialog.findViewById(R.id.add_fold_btn);
final Button back = (Button) dialog.findViewById(R.id.back_btn);
final EditText titre = (EditText) dialog.findViewById(R.id.titre);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
//toast.cancel();
}
});
add_dos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dossier dos = new Dossier(titre.getText().toString());
dosViewModel.insert(dos);
dialog.dismiss();
}
});
dialog.show();
dialog.getWindow().setAttributes(lp);
}
}
FolderViewModel
public class DosViewModel extends AndroidViewModel {
private DosRepository dosRepository;
private LiveData<List<Dossier>> dossiers;
public DosViewModel(@NonNull Application application) {
super(application);
dosRepository = new DosRepository(application);
dossiers = dosRepository.getAllFolders();
}
public void insert(Dossier dossier) { dosRepository.insert(dossier); }
public void update(Dossier dossier) {
dosRepository.update(dossier);
}
public void delete(Dossier dossier) {
dosRepository.delete(dossier);
}
public void deleteAllFolders() { dosRepository.deleteAllFolders(); }
public LiveData<List<Dossier>> getAllFolders() {
return dossiers;
}
}
FolderRepository
public class DosRepository {
private DosDAO dosDAO;
private LiveData<List<Dossier>> dossiers;
public DosRepository(Application application) {
DB_MJG database = DB_MJG.getInstance(application);
dosDAO= database.dosDAO();
dossiers = dosDAO.getAllFolders();
}
public void insert(Dossier dossier) {
new InsertDosAsyncTask(dosDAO).execute(dossier);
}
public void update(Dossier dossier) {
new UpdateDosAsyncTask(dosDAO).execute(dossier);
}
public void delete(Dossier dossier) {
new DeleteDosAsyncTask(dosDAO).execute(dossier);
}
public void deleteAllFolders() {
new DeleteAllDosAsyncTask(dosDAO).execute();
}
public LiveData<List<Dossier>> getAllFolders() {
return dossiers;
}
private static class InsertDosAsyncTask extends AsyncTask<Dossier, Void,
Void> {
private DosDAO dosDAO;
private InsertDosAsyncTask(DosDAO dosDAO) {
this.dosDAO =dosDAO;
}
@Override
protected Void doInBackground(Dossier... dossiers) {
dosDAO.insert(dossiers[0]);
return null;
}
}
private static class UpdateDosAsyncTask extends AsyncTask<Dossier, Void,
Void> {
private DosDAO dosDAO;
private UpdateDosAsyncTask(DosDAO dosDAO) {
this.dosDAO = dosDAO;
}
@Override
protected Void doInBackground(Dossier... dossiers) {
dosDAO.update(dossiers[0]);
return null;
}
}
private static class DeleteDosAsyncTask extends AsyncTask<Dossier, Void,
Void> {
private DosDAO dosDAO;
private DeleteDosAsyncTask(DosDAO dosDAO) {
this.dosDAO = dosDAO;
}
@Override
protected Void doInBackground(Dossier... dossiers) {
dosDAO.delete(dossiers[0]);
return null;
}
}
private static class DeleteAllDosAsyncTask extends AsyncTask<Void, Void,
Void> {
private DosDAO dosDAO;
private DeleteAllDosAsyncTask(DosDAO dosDAO) {
this.dosDAO = dosDAO;
}
@Override
protected Void doInBackground(Void... voids) {
dosDAO.deleteAllFolders();
return null;
}
}
}
база данных
Database(entities = {Note.class,RDV.class, Dossier.class}, version = 2)
public abstract class DB_MJG extends RoomDatabase {
private static DB_MJG instance;
public abstract NoteDAO noteDao();
public abstract DosDAO dosDAO();
public static synchronized DB_MJG getInstance(Context context)
{
if(instance==null){
instance = Room.
databaseBuilder(context.getApplicationContext(),DB_MJG.class,"MJGbd")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
return instance;
}
private static RoomDatabase.Callback roomCallback = new
RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
new PopulateDbAsyncTask(instance).execute();
}
};
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void,
Void>
{
private NoteDAO noteDAO;
private DosDAO dosDAO;
private PopulateDbAsyncTask(DB_MJG db) {
noteDAO = db.noteDao();
dosDAO = db.dosDAO();
}
@Override
protected Void doInBackground(Void... voids) {
noteDAO.insert(new Note("20/05/2019", "23:50", "Je t'aime my
liitle boy"));
noteDAO.insert(new Note("21/05/2019", "23:50", "Je t'aime my
liitle girl"));
dosDAO.insert(new Dossier(":3"));
dosDAO.insert(new Dossier("Zmer"));
return null;
}
}
}