I am implementing a local database using ROOM in android studio. I am having a problem. I am successful in inserting and deleting the data entries into the database. But when I am getting all the data objects from the database then each time I am getting one object less. When that activity starts then there is no problem but when I am trying to update the recycler view after inserting a new data object into the database then that object is being updated into the recycler view one step after creating a new data object entry and the same problem happens with this also.
below is my complete code:-
package com.example.eventus.DashBoard;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import com.example.eventus.ApplicationDatabase.Events.Event;
import com.example.eventus.ApplicationDatabase.Events.EventsAdapter;
import com.example.eventus.ApplicationDatabase.Events.EventsDao;
import com.example.eventus.R;
import com.example.eventus.Singletons.DatabaseSingleton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Dashboard extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
//Declaring the views
private FloatingActionButton fbAddEvent;
private Button btnOKNewEvent, btnCancelNewEvent;
private EditText etDeadlineNewEvent, etTitleNewEvent, etInfoNewEvent;
//For recycler view
private RecyclerView rvEvents;
private RecyclerView.LayoutManager layoutManager;
private List<Event> events;
private EventsAdapter eventsAdapter;
//For alert box
private AlertDialog.Builder alertDialogBuilder;
private AlertDialog alertDialog;
private View alertDialogView;
//For database
private DatabaseSingleton databaseSingleton;
private EventsDao eventsDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//Initializing and setting onclicklisteners on views
init();
//RecyclerView stuff
recyclerViewStuff();
//new event stuff
newEventDialog();
//database stuff
database();
//loadEvents
loadEvents();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.fb_add_event_dashboard:
alertDialog.show();
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = DateFormat.getDateInstance(DateFormat.FULL).format(mCalendar.getTime());
etDeadlineNewEvent.setText(selectedDate);
}
private void init(){
//Initializing the views
rvEvents = findViewById(R.id.rv_events_dashboard);
fbAddEvent = findViewById(R.id.fb_add_event_dashboard);
//Setting onclicklisteners on views
fbAddEvent.setOnClickListener(this);
}
private void recyclerViewStuff() {
//Initializing the recycler view stuffs
layoutManager = new GridLayoutManager(this, 2);
rvEvents.setLayoutManager(layoutManager);
}
private void newEventDialog() {
alertDialogBuilder = new AlertDialog.Builder(Dashboard.this);
alertDialogView = getLayoutInflater().inflate(R.layout.new_event, null);
alertDialogBuilder.setView(alertDialogView);
alertDialog = alertDialogBuilder.create();
alertDialog.setCanceledOnTouchOutside(false);
//Instantiating the view
btnOKNewEvent = alertDialogView.findViewById(R.id.btn_ok_new_event);
etDeadlineNewEvent = alertDialogView.findViewById(R.id.et_deadline_new_event);
btnCancelNewEvent = alertDialogView.findViewById(R.id.btn_cancel_new_event);
etTitleNewEvent = alertDialogView.findViewById(R.id.et_title_new_event);
etInfoNewEvent = alertDialogView.findViewById(R.id.et_info_new_event);
//setting on click listeners for the views in dialog box
btnOKNewEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addEvent();
//loadEvents();
refreshEvents();
alertDialog.dismiss();
}
});
btnCancelNewEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
etDeadlineNewEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new com.example.eventus.DateAndTime.DatePicker();
datePicker.show(getSupportFragmentManager(), null);
}
});
}
private void database(){
//getting the database instance
databaseSingleton = DatabaseSingleton.getInstance();
//getting the events dao
eventsDao = databaseSingleton.getEventsDao(getApplicationContext());
//deleting all previously saved events
eventsDao.deleteAllEvent();
}
private void addEvent() {
String eventTitle, eventInfo, eventDeadline;
eventTitle = etTitleNewEvent.getText().toString();
eventInfo = etInfoNewEvent.getText().toString();
eventDeadline = etDeadlineNewEvent.getText().toString();
//creating an event
Event event = new Event(eventTitle, eventInfo, eventDeadline);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
eventsDao.createEvent(event);
Toast.makeText(Dashboard.this, "Event Created", Toast.LENGTH_SHORT).show();
}
};
handler.post(runnable);
}
private void loadEvents(){
events = eventsDao.getEvents();
eventsAdapter = new EventsAdapter(events);
rvEvents.setAdapter(eventsAdapter);
}
private void refreshEvents(){
events.clear();
List<Event> tempEventsList = eventsDao.getEvents();
for(int i=0;i<tempEventsList.size();i ){
events.add(tempEventsList.get(i));
}
eventsAdapter.notifyDataSetChanged();
}
}
CodePudding user response:
The problem you are having is realted to concurrency. If you check your code you first call the add event method and place the insertion inside a Runnable, this tells the application to execute at a later time when possible, but it does not execute it inmediately; therefor the code continues its execution, you fetch the events and then, at a later time, the new event is added.
To solve your problem, try to place the call of the method refreshEvents inside the runnable.
Runnable runnable = new Runnable() {
@Override
public void run() {
eventsDao.createEvent(event);
Toast.makeText(Dashboard.this, "Event Created", Toast.LENGTH_SHORT).show();
refreshEvents();
}
};
handler.post(runnable);
EDIT: As correctly said in the comments is not a mult-thread issue, it's the execution time of code fragments inside a single thread.