мое приложение добавляет элемент к Recyclerview
в 12 часов утра, оно добавляется, а затем исчезает, когда OnPause
активности здесь - мой код, и я не знаю, было ли оно удалено из моего RoomDatabase
или из моего RecyclerView
ListOfItemsActivity. java
RecyclerView notesRecyclerView;
NotesRecyclerViewAdapter notesRecyclerViewAdapter;
RecyclerView.LayoutManager layoutManager;
NotesViewModel notesViewModel;
List<Note> note = new ArrayList<>();
private static final String PREF_PAUSE_TIME_KEY = "exit_time";
private static final Long MILLIS_IN_DAY = 86400000L;
private static final int TRIGGER_HOUR = 0;
private static final int TRIGGER_MIN = 0;
private static final int TRIGGER_SEC = 0;
private final Handler handler = new Handler();
private SharedPreferences prefs;
private final Calendar calendar = Calendar.getInstance();
private final Runnable addItemRunnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(addItemRunnable, MILLIS_IN_DAY);
addNote();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recorded_notes);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
protected void onResume() {
super.onResume();
Log.e("state","onResumeStarted");
// Add missing events since onPause.
long resumeTime = System.currentTimeMillis();
long pauseTime = prefs.getLong(PREF_PAUSE_TIME_KEY, resumeTime);
// Set calendar to trigger time on the day the app was paused.
calendar.setTimeInMillis(pauseTime);
calendar.set(Calendar.HOUR_OF_DAY, TRIGGER_HOUR);
calendar.set(Calendar.MINUTE, TRIGGER_MIN);
calendar.set(Calendar.SECOND, TRIGGER_SEC);
long time;
while (true) {
// If calendar time is during the time that app was on pause, add item.
time = calendar.getTimeInMillis();
if (time > resumeTime) {
// Past current time, all items were added.
addNote();
break;
} else if (time >= pauseTime) {
// This time happened when app was on pause, add item.
fillingRecyclerView();
}
// Set calendar time to same hour on next day.
calendar.add(Calendar.DATE, 1);
}
// Set handler to add item on trigger time.
handler.postDelayed(addItemRunnable, time - resumeTime);
}
@Override
protected void onPause() {
super.onPause();
Log.e("state","onPauseStarted");
// Save pause time so items can be added on resume.
prefs.edit().putLong(PREF_PAUSE_TIME_KEY, System.currentTimeMillis()).apply();
// Cancel handler callback to add item.
handler.removeCallbacks(addItemRunnable);
}
public void addNote(){
SimpleDateFormat sdf = new SimpleDateFormat("E", new Locale("ar"));
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy", new Locale("ar"));
final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String[] states = new String[6];
MainSharedPrefes mainSharedPrefes = new MainSharedPrefes(this);
if (mainSharedPrefes.getMorningState()){
states[0] = "+";
}else {
states[0] = "-";
}
if(mainSharedPrefes.getEveningState()){
states[1] = "+";
}else {
states[1] = "-";
}
if (mainSharedPrefes.getNightState()){
states[2] = "+";
}else {
states[2] = "-";
}
if (mainSharedPrefes.getEchauristState()){
states[3] = "+";
}else {
states[3] = "-";
}
if (mainSharedPrefes.getConfessState()){
states[4] = "+";
}else {
states[4] = "-";
}
if (mainSharedPrefes.getBibleState()){
states[5] = "+";
}else {
states[5] = "-";
}
note.add(new Note(sdf.format(timestamp),sdf2.format(timestamp),states[0],states[1],states[2],states[3],states[4],states[4],mainSharedPrefes.getWritedNotes()));
}
public void fillingRecyclerView(){
notesViewModel = ViewModelProviders.of(this).get(NotesViewModel.class);
layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
notesRecyclerViewAdapter = new NotesRecyclerViewAdapter(note);
notesRecyclerView = findViewById(R.id.notesRecyclerView);
notesRecyclerView.setHasFixedSize(true);
notesRecyclerView.setLayoutManager(layoutManager);
notesRecyclerView.setAdapter(notesRecyclerViewAdapter);
}
Адаптер. java
private List<Note> notesList = new ArrayList<>();
NotesRecyclerViewAdapter(List<Note> notesList) {
this.notesList = notesList;
notifyDataSetChanged();
}
@NonNull
@Override
public NotesRecyclerViewAdapter.NotesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note, parent, false);
return new NotesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NotesRecyclerViewAdapter.NotesViewHolder holder, int position) {
Note currentNote = notesList.get(position);
holder.day.setText(currentNote.getDay());
holder.date.setText(currentNote.getDate());
holder.morningState.setText(currentNote.getMorningState());
holder.eveningState.setText(currentNote.getEveningState());
holder.nightState.setText(currentNote.getNightState());
holder.echuaristState.setText(currentNote.getEucharistState());
holder.confessState.setText(currentNote.getConfessState());
holder.bibleState.setText(currentNote.getBibleState());
holder.notes.setText(currentNote.getNotes());
}
@Override
public int getItemCount() {
return notesList.size();
}
class NotesViewHolder extends RecyclerView.ViewHolder {
TextView date;
TextView day;
TextView morningState;
TextView eveningState;
TextView nightState;
TextView echuaristState;
TextView confessState;
TextView bibleState;
TextView notes;
NotesViewHolder(@NonNull View itemView) {
super(itemView);
date = itemView.findViewById(R.id.dateInNote);
day = itemView.findViewById(R.id.dayInNote);
morningState = itemView.findViewById(R.id.morningState);
eveningState = itemView.findViewById(R.id.eveningState);
nightState = itemView.findViewById(R.id.nightState);
echuaristState = itemView.findViewById(R.id.echuaristState);
confessState = itemView.findViewById(R.id.confessState);
bibleState = itemView.findViewById(R.id.bibleState);
notes = itemView.findViewById(R.id.notesInRecordedNotes);
}
}
AppDatabase. java
@Database(entities = {Note.class}, version = 1, exportSchema = false)
public abstract class NotesDatabase extends RoomDatabase {
private static NotesDatabase instance;
public abstract Dao dao();
public static synchronized NotesDatabase getInstance(Context context){
if(instance == null){
instance = Room.databaseBuilder(context.getApplicationContext(), NotesDatabase.class, "notes_database").fallbackToDestructiveMigration().build();
}
return instance;
}
private static RoomDatabase.Callback callback = new RoomDatabase.Callback(){
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
}
};
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void>{
private Dao dao;
public PopulateDbAsyncTask(NotesDatabase database) {
dao = database.dao();
}
@Override
protected Void doInBackground(Void... voids) {
return null;
}
}
надеюсь, кто-нибудь мне поможет