Room database error while deleting row onclick - android

I have defined a room SQLite database as:
#Database(entities = {PlaceSaved.class},version = 1)
public abstract class PlaceDatabase extends RoomDatabase {
public abstract DatabaseInterface databaseInterface();
#Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
return null;
}
#Override
protected InvalidationTracker createInvalidationTracker() {
return null;
}
}
with definitions at:
#Entity
public class PlaceSaved {
#PrimaryKey(autoGenerate = true)
private int id;
#ColumnInfo(name = "time")
private String time;
#ColumnInfo(name="title")
private String title;
public PlaceSaved(){
}
public PlaceSaved(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
the corresponding DAO is:
#Dao
public interface DatabaseInterface {
#Query("SELECT * FROM placesaved")
List<PlaceSaved> getAllItems();
#Insert
void insertAll(PlaceSaved... todoListItems);
#Delete
public void delete(PlaceSaved... todoListItems);
#Update
public void update(PlaceSaved...todoListItems);
}
and those data are displayed via a recyclerview with each item layout defined as:
<TextView
android:id="#+id/secondLine"/>
<TextView
android:id="#+id/firstLine"/>
<ImageButton
android:id="#+id/delicon"/>
Now, I want to use this delicon ImageButton to delete the corresponding entry.
So, I tried to put this in my Adapter as (NOTE: Updated, see Updated code at the end):
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {
//PlaceDatabase db;
List<PlaceSaved> items;
public PlacesAdapter(List<PlaceSaved> items) {
this.items = items;
}
#Override
public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_list_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(PlacesAdapter.ViewHolder holder, final int position) {
holder.name.setText(items.get(position).getTitle());
holder.time.setText(items.get(position).getTime());
// holder.delbutton.setClickable(true);
holder.delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeItem(items);
}
});
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView time;
public ImageButton delbutton;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.secondLine);
time= itemView.findViewById(R.id.firstLine);
delbutton = itemView.findViewById(R.id.delicon);
}
}
private void removeItem(PlaceSaved infoItem){
PlaceSaved placeSaved = new PlaceSaved();
placeSaved.delete(infoItem);
}
}
and the recyclerview is called as: (**Note: onCreate is updated and posted at the end)
public class PlacesActivity extends AppCompatActivity {
FloatingActionButton fab, fab1, fab2, fab3;
LinearLayout fabLayout1, fabLayout2, fabLayout3;
boolean isFABOpen=false;
View fabBGLayout;
public static RecyclerView recyclerView;
public static RecyclerView.Adapter adapter;
List<PlaceSaved> items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
//whenever the activity is started, it reads data from database and stores it into
// local array list 'items'
final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class, "production")
.build();
//it is very bad practice to pull data from Room on main UI thread,
// that's why we create another thread which we use for getting the data and displaying it
Runnable r = new Runnable() {
#Override
public void run() {
items = db.databaseInterface().getAllItems();
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
adapter = new PlacesAdapter(items);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
};
This gives compilation time error:
PlacesAdapter.java
Error:(43, 22) error: incompatible types: List<PlaceSaved> cannot be converted to PlaceSaved
Error:(68, 17) error: cannot find symbol method delete(PlaceSaved)
Kindly help me to solve this problem.
UPDATE
By Vishu's answer, I have updated my adapter as:
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {
private static final String TAG = "MyActivity";
List<PlaceSaved> items;
PlaceDatabase db;
public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
this.items = items;
this.db = db;
}
#Override
public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.places_list_item,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final PlacesAdapter.ViewHolder holder, final int position) {
holder.name.setText(items.get(position).getTitle());
holder.time.setText(items.get(position).getTime());
holder.delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeItem(items.get(holder.getAdapterPosition()));
}
});
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView time;
public ImageButton delbutton;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.secondLine);
time= itemView.findViewById(R.id.firstLine);
delbutton = itemView.findViewById(R.id.delicon);
}
}
private void removeItem(PlaceSaved infoItem){
// db.delete(infoItem);
Log.v(TAG, "remove Item called");
}
}
and in PlacesActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
final PlaceDatabase db = Room.databaseBuilder(getApplicationContext(), PlaceDatabase.class, "production")
.build();
Runnable r = new Runnable() {
#Override
public void run() {
items = db.databaseInterface().getAllItems();
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
adapter = new PlacesAdapter(items, db);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
};
which is still giving syntax error :
Error:(71, 17) error: cannot find symbol method delete(PlaceSaved)
and 2 warning (not due to Vishu's answer, it was present before):
PlaceSaved.java
Warning:(11, 8) There are multiple good constructors and Room will pick the no-arg constructor. You can use the #Ignore annotation to eliminate unwanted constructors.
PlaceDatabase.java
Warning:(13, 17) Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
Error: Update:
adding db.databaseInterface().delete(infoItem);
in removeitem gives:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at android.arch.persistence.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:164)
at android.arch.persistence.room.RoomDatabase.beginTransaction(RoomDatabase.java:211)
at DatabaseInterface_Impl.delete(DatabaseInterface_Impl.java:94)
at PlacesAdapter.removeItem(PlacesAdapter.java:69)
at PlacesAdapter.access$000(PlacesAdapter.java:20)
at PlacesAdapter$1.onClick(PlacesAdapter.java:45)
at android.view.View.performClick(View.java:6294)

why are you passing ArrayList of PlaceSaved in removeItem(). removeItem() accept only PlaceSaved.
So change your onclick to
removeItem(items.get(holder.getAdapterPosition()));
make holder final in method signature final PlacesAdapter.ViewHolder holder else it won't compile.
You have defined delete in PlaceDatabase and you are calling on PlaceSaved that's why you are getting Error:(68, 17) error: cannot find symbol method delete(PlaceSaved)
you can pass db from in PlacesAdapter just like items like PlacesAdapter(items, db)
change
adapter = new PlacesAdapter(items);
to
adapter = new PlacesAdapter(items, db);
Now your PlacesAdapter will have db instance. and you can replce placeSaved.delete(infoItem); with db.delete(infoItem)
Change
List<PlaceSaved> items;
public PlacesAdapter(List<PlaceSaved> items) {
this.items = items;
}
to
List<PlaceSaved> items;
PlaceDatabase db;
public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
this.items = items;
this.db = db
}
And
Change
placeSaved.delete(infoItem);
to
db.databaseInterface().delete(infoItem);

Related

Can't retrieve data from cloud firestore firebase

I send data from the activity to the cloud firestore and I retrieve it in the second activity in recycler view.
but the data doesn't appear in the second activity.
I used FirestoreRecyclerAdapter and FirestoreOptions.
This is the activity in which I retrieve the data.
public class MyServicesActivity extends AppCompatActivity {
private FirestoreRecyclerAdapter adapter;
private RecyclerView recyclerView;
private FirebaseFirestore db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_services);
db = FirebaseFirestore.getInstance();
Query query = db.collection("Services Requested");
recyclerView = findViewById(R.id.rv_my_services);
FirestoreRecyclerOptions<ServiceModel> response = new FirestoreRecyclerOptions
.Builder<ServiceModel>()
.setQuery(query, ServiceModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<ServiceModel, ServiceHolder>(response) {
#NonNull
#Override
public ServiceHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_design, parent, false);
return new ServiceHolder(v);
}
#SuppressLint("SetTextI18n")
#Override
protected void onBindViewHolder(#NonNull ServiceHolder holder, int position, #NonNull ServiceModel model) {
holder.serviceImage.setImageResource(model.getServiceImage());
Log.d("DATA", "data isn't null" + position);
holder.serviceName.setText(model.getServiceName());
holder.servicePrice.setText(model.getPrice() + "" + " L.E");
}
};
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public class ServiceHolder extends RecyclerView.ViewHolder {
private ImageView serviceImage;
private TextView serviceName, servicePrice;
public ServiceHolder(#NonNull View itemView) {
super(itemView);
serviceImage = itemView.findViewById(R.id.polish_img);
serviceName = itemView.findViewById(R.id.polish_txt);
servicePrice = itemView.findViewById(R.id.price_txt);
}
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.startListening();
}
}
This is the class model for retrieve data from cloud firestore
public class ServiceModel {
private String serviceName;
private int price;
private int serviceImage;
public ServiceModel(){}
public ServiceModel(String serviceName, int price, int serviceImage) {
this.serviceName = serviceName;
this.price = price;
this.serviceImage = serviceImage;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public void setPrice(int price) {
this.price = price;
}
public void setServiceImage(int serviceImage) {
this.serviceImage = serviceImage;
}
public String getServiceName() {
return serviceName;
}
public int getPrice() {
return price;
}
public int getServiceImage() {
return serviceImage;
}
}
I see in your screenshot, that the name of the properties in the database are stored using whitespace between the words, and each word is starting with a capital letter, while in your class, there is no whitespace and the first word starts with a lower-case and the second with a capital letter. When you are using a public getter called getServiceName(), Firebase is looking in the database after a property called serviceName, which actually does not exist.
To solve this, you either change the properties in your database to match the one in the class, remove the actual data, and add a fresh one, or you can use an annotation called PropertyName in front of the getter, to match the actual naming.

How to check if an item already exists in a Room Database?

I have this adapter with a button that dependent on if a object already exists in my Room Database or not will have a differente behavior when it´s clicked. Basically what I want to do is if the object exists I want to remove it. In case it doesn´t I want to add it to my database. I created this method in Dao and a Task to check the existence. Since the Task is asynchronous ,how can I do the verification?
My Adapter
public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.RestaurantViewHolder> {
private Context mContext;
private List<Restaurant_> mRestaurants;
private Activity act;
private FirebaseAuth mAuth;
private String currentUserId;
private View rView;
public RestaurantAdapter(Context context, List<Restaurant_> restaurants, Activity activity) {
mRestaurants = restaurants;
mContext = context;
act = activity;
}
#Override
public RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Get layout inflater from context
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate layout
rView = inflater.inflate(R.layout.item_restaurant, parent, false);
// Return a new holder instance
return new RestaurantViewHolder(rView);
}
#Override
public void onBindViewHolder(RestaurantViewHolder viewHolder, final int position) {
// Get the data model based on position
final Activity activity = act;
final Restaurant_ restaurant = mRestaurants.get(position);
mAuth = FirebaseAuth.getInstance();
currentUserId = mAuth.getCurrentUser().getUid();
final TextView name = viewHolder.nameTextView;
name.setText(restaurant.getName());
final TextView rating = viewHolder.ratingTextView;
rating.setText((restaurant.getUserRating().getAggregateRating()));
final TextView distance = viewHolder.distanceTextView;
distance.setText(String.valueOf(restaurant.getDistance()) + " Km");
final ImageButton addToWishlistButton = viewHolder.addToWishlistButton;
addToWishlistButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Wishlist wishlist = new Wishlist(currentUserId, restaurant.getId());
//if(alreadyExists){
//RemoveWLTask rlt=new RemoveWLTask(wishlist,activity);
//rlt.execute()
// }
// else{
AddWLTask wlt = new AddWLTask(wishlist, activity);
wlt.execute();
//}
}
});
final ImageButton addToFavoritesButton = viewHolder.addToFavoritesButton;
addToFavoritesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addToFavoritesButton.getBackground().setTint(activity.getResources().getColor(R.color.red));
addToFavorites();
}
});
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LiveFragment.getListener().onRestaurantClicked(restaurant.getId());
}
});
}
#Override
public int getItemCount() {
return mRestaurants.size();
}
public class RestaurantViewHolder extends RecyclerView.ViewHolder {
public TextView nameTextView;
public TextView ratingTextView;
public TextView distanceTextView;
public ImageButton addToWishlistButton;
public ImageButton addToFavoritesButton;
public RestaurantViewHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.restaurantName);
ratingTextView = itemView.findViewById(R.id.restaurantRating);
distanceTextView = itemView.findViewById(R.id.restaurantDistance);
addToWishlistButton = itemView.findViewById(R.id.button_wishlist);
addToFavoritesButton = itemView.findViewById(R.id.button_favorites);
}
}
}
My DAO
#Dao
public interface DAO {
#Insert
public void addToWishlist(Wishlist wishlist);
#Delete
public void deleteFromWishlist(Wishlist wishlist);
#Query("Select restaurantId From wishlist Where userId=:id")
public String[] loadWishlist(String id);
#Query("Select restaurantId From wishlist where userId=:userID AND restaurantId=:restaurantID")
public String[]checkExists(String userID, String restaurantID);
}
##My Task ##
public class CheckWLTask extends AsyncTask<Void, Void, Void> {
private DB db;
private Activity activity;
private String userId;
private String restaurantId;
private String [] response;
public CheckWLTask(Activity activity, String idUser, String idRestaurant) {
this.activity = activity;
this.userId = idUser;
this.restaurantId = idRestaurant;
db = Room.databaseBuilder(activity.getApplicationContext(), DB.class, "sample-db").build();
}
#Override
protected Void doInBackground(Void... voids) {
while (!isCancelled()) {
this.response=db.daoAcess().checkExists(userId,restaurantId);
break;
}
return null;
}
}
``

Using FirestoreRecyclerAdapter but cannot query any data from Firebase

Base on guideline at https://github.com/firebase/FirebaseUI-Android/tree/master/firestore, I make a list items with FirestoreRecyclerAdapter but I don't know why the couldn't retrieve any data. In fact, the number view items are shown correctly, but the content always null. Anyone can help me. Below is source code:
Activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseFirestore mDatabase;
private ViewPager mViewPager;
private CollectionReference mOrderRef;
private OrderAdapter mOrderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_order_list);
mDatabase = FirebaseFirestore.getInstance();
mOrderRef = mDatabase.collection("order");
Query query = mOrderRef.limit(1000);
FirestoreRecyclerOptions<OrderInfo> options = new FirestoreRecyclerOptions.Builder<OrderInfo>().setQuery(query, OrderInfo.class).build();
mOrderAdapter = new OrderAdapter(options);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view_order_list);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mOrderAdapter);
}
#Override
protected void onStart() {
super.onStart();
mOrderAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mOrderAdapter.stopListening();
}
Adapter:
public class OrderAdapter extends FirestoreRecyclerAdapter<OrderInfo, OrderAdapter.OrderHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {#link
* FirestoreRecyclerOptions} for configuration options.
*
* #param options
*/
public OrderAdapter(#NonNull FirestoreRecyclerOptions<OrderInfo> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull OrderHolder holder, int position, #NonNull OrderInfo model) {
holder.txtTitle.setText(String.valueOf(model.getTitle()));
holder.txtCusName.setText(String.valueOf(model.getCusName()));
holder.txtDate.setText(String.valueOf(model.getDate()));
}
#NonNull
#Override
public OrderHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item, parent, false);
return new OrderHolder(view);
}
class OrderHolder extends RecyclerView.ViewHolder{
TextView txtCusName;
TextView txtTitle;
TextView txtDate;
public OrderHolder(#NonNull View itemView) {
super(itemView);
txtCusName = itemView.findViewById(R.id.txt_customer_name);
txtTitle = itemView.findViewById(R.id.txt_title);
txtDate = itemView.findViewById(R.id.txt_date);
}
}
}
Data model:
public class OrderInfo {
private String mTitle;
private String mCusName;
private String mDate;
public OrderInfo(){
//default constructor
}
public OrderInfo(String title, String name, String date) {
this.mTitle = title;
this.mCusName = name;
this.mDate = date;
}
public String getTitle() {
return mTitle;
}
public String getCusName() {
return mCusName;
}
public void setCusName(String cusName) {
this.mCusName = cusName;
}
public String getDate() {
return mDate;
}
}
my database structure:
You data model is not aligned with database structure. Try to update it like below:
public class OrderInfo {
private String title;
private String name;
private String date;
public OrderInfo(){
//default constructor
}
public OrderInfo(String title, String name, String date) {
this.title = title;
this.name = name;
this.date = date;
}
public String getTitle() {
return title;
}
public String getName() {
return name;
}
public String getDate() {
return date;
}
}
Besides this don't forget to sync your project with firebase firestore.

Unable to retrieve firestore database using recyclerView

I am using recyclerView and Adapter to fetch the data in profileActivity
here is my
public class studentDetailsRecyclerActivity extends AppCompatActivity {
//recyclerview to set the details for UI in the student profile activity
private RecyclerView mRecyclerView;
private storeDetailsAdapter mStoreDetailsAdapter;
private List<storeStudentDetails> studentDetailsList;
private FirebaseFirestore dbReference;
private ProgressBar mProgressBar;
private String TAG = studentDetailsRecyclerActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
dbReference = FirebaseFirestore.getInstance();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_details);
mProgressBar = findViewById(R.id.progressbar);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_products);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
studentDetailsList = new ArrayList<>();
mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);
mRecyclerView.setAdapter(mStoreDetailsAdapter);
//to get the "details" this is our collection from firestore so we must fetch them
//by calling the addOnSuccessListener
dbReference.collection("details").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) { //we must have to hide the progress bar when the data gets loaded
//here queryDocumentsSnapshot will hold all the "details" which is your collection in firestore
if(!queryDocumentSnapshots.isEmpty()){
//we must have to create empty list so that to store all
//details from DocumentsSnapshots
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
//enhanced for loop because we have to give every index documentSnapShot
for(DocumentSnapshot d: list){
storeStudentDetails sd = d.toObject(storeStudentDetails.class);
studentDetailsList.add(sd);
Log.d(TAG, "onSuccess: " + sd.toString());
}
//to refresh and sync we must have to use notifyDataSetChanged
mStoreDetailsAdapter.notifyDataSetChanged();
}
}
}) .addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Error getting data!!!", Toast.LENGTH_LONG).show();
}
});
}
}
and here is my storeDetailsAdapter
import java.util.List;
public class storeDetailsAdapter extends RecyclerView.Adapter<storeDetailsAdapter.StudentViewHolder>{
private Context context;
private List<storeStudentDetails> studentDetailsList;
public storeDetailsAdapter(Context context, List<storeStudentDetails> studentDetailsList) {
this.context = context;
this.studentDetailsList = studentDetailsList;
}
#NonNull
#Override
public StudentViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new StudentViewHolder(
LayoutInflater.from(context).inflate(R.layout.profile_activity, parent, false)
);
}
#Override
public void onBindViewHolder(#NonNull StudentViewHolder holder, int position) {
storeStudentDetails mStoreDetails = studentDetailsList.get(position);
holder.studName.setText(mStoreDetails.getStudentName());
holder.rollNum.setText(mStoreDetails.getRollNo());
holder.bookName.setText( mStoreDetails.getBook());
holder.fine.setText("Fine:" + mStoreDetails.getFine());
holder.dept.setText(mStoreDetails.getDept());
}
#Override
public int getItemCount() {
return studentDetailsList.size();
}
class StudentViewHolder extends RecyclerView.ViewHolder {
TextView studName,rollNum,bookName,dept,fine;
public StudentViewHolder(View itemView) {
super(itemView);
studName=itemView.findViewById(R.id.studentName_prof);
rollNum = itemView.findViewById(R.id.rollNumber_prof);
bookName = itemView.findViewById(R.id.bookName_prof);
fine = itemView.findViewById(R.id.fineAmt_prof);
dept = itemView.findViewById(R.id.department_prof);
}
}
}
and here is my StoreStudentDetails class:
public class storeStudentDetails implements Serializable {
private String studentName;
private String rollNo;
private String book;
private Double fine;
private String dept;
#Exclude private String id;
public storeStudentDetails() {
}
public storeStudentDetails(String studentName, String rollNo,String book, double fine ,String dept) {
this.studentName = studentName;
this.rollNo = rollNo;
this.book = book;
this.fine = fine;
this.dept = dept;
}
public void setId(String id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public String getRollNo() {
return rollNo;
}
public String getBook() {
return book;
}
public Double getFine() {
return fine;
}
public String getDept() {
return dept;
}
public String getId() {
return id;
}
}
To solve this, please move the following lines of code:
mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);
mRecyclerView.setAdapter(mStoreDetailsAdapter);
Right before the following line of code:
mStoreDetailsAdapter.notifyDataSetChanged();
And this is because onSuccess() method has an asynchronous behavior and by the time you are setting the adapter outside the callback your list is empty.
As you can see, the easiest solution for this problem is to move those lines of code inside the callback. but if you want to use the value of your studentDetailsList outside the onSuccess() method, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

how to update database using room persistance library and livedata

I am trying to update my database using Room persistence Library and Livedata. I am very new to java, so, with the manual and various tutorial, I have setup the DAO, entity etc. But I am still struggling with how to actually add the data.
This is my database defination:
#Entity
public class PlaceSaved {
#PrimaryKey(autoGenerate = true)
private int id;
private String place;
private String lati;
private String longi;
public PlaceSaved(String place, String lati, String longi) {
this.place = place;
this.lati = lati;
this.longi = longi;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getLongi() {
return longi;
}
public void setLongi(String longi) {
this.longi = longi;
}
public String getLati() {
return lati;
}
public void setLati(String lati) {
this.lati = lati;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
DAO
#Dao
public interface DatabaseInterface {
#Query("SELECT * FROM placesaved")
LiveData<List<PlaceSaved>> getAllItems();
#Insert
void insertAll(PlaceSaved... placeSaveds);
#Delete
void delete(PlaceSaved... placeSaveds);
#Update
void update(PlaceSaved... placeSaveds);
}
Adapter
public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.RecyclerViewHolder>{
private List<PlaceSaved> items;
private View.OnClickListener ClickListener;
public PlacesAdapter(List<PlaceSaved> items){//}, View.OnClickListener ClickListener) {
this.items = items;
//this.ClickListener = ClickListener;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.places_list_item, parent, false));
}
#Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
PlaceSaved placeSaved = items.get(position);
holder.itemTextView.setText(placeSaved.getPlace());
holder.nameTextView.setText(placeSaved.getLati());
holder.dateTextView.setText(placeSaved.getLongi());
/* holder.dateTextView.setText(borrowModel.getBorrowDate().toLocaleString().substring(0, 11));
holder.itemView.setTag(borrowModel);
holder.itemView.setOnLongClickListener(longClickListener);*/
}
#Override
public int getItemCount() {
return items.size();
}
public void addItems(List<PlaceSaved> items) {
this.items = items;
notifyDataSetChanged();
}
static class RecyclerViewHolder extends RecyclerView.ViewHolder {
private TextView itemTextView;
private TextView nameTextView;
private TextView dateTextView;
RecyclerViewHolder(View view) {
super(view);
itemTextView = (TextView) view.findViewById(R.id.firstLine);
nameTextView = (TextView) view.findViewById(R.id.secondLine);
dateTextView = (TextView) view.findViewById(R.id.longitude);
}
}
}
and the ViewModel
public class PlacesViewModel extends AndroidViewModel {
private final LiveData<List<PlaceSaved>> PlacedatabaseList;
private PlaceDatabase appDatabase;
public PlacesViewModel(Application application) {
super(application);
appDatabase = PlaceDatabase.getDatabase(this.getApplication());
PlacedatabaseList = appDatabase.PlacedatabaseInterface().getAllItems();
}
public LiveData<List<PlaceSaved>> getPlaceList() {
return PlacedatabaseList;
}
public void deleteItem(PlaceSaved placeSaved) {
new deleteAsyncTask(appDatabase).execute(placeSaved);
}
private static class deleteAsyncTask extends AsyncTask<PlaceSaved, Void, Void> {
private PlaceDatabase db;
deleteAsyncTask(PlaceDatabase appDatabase) {
db = appDatabase;
}
#Override
protected Void doInBackground(final PlaceSaved... params) {
db.PlacedatabaseInterface().delete(params[0]);
return null;
}
}
}
Now, I am trying to add an item to database with OnClick of a fab as:
public class PlacesActivity extends AppCompatActivity {
private PlacesViewModel viewModel;
private PlacesAdapter placesAdapter;
private RecyclerView recyclerView;
FloatingActionButton fab, fab1, fab2, fab3;
LinearLayout fabLayout1, fabLayout2, fabLayout3;
boolean isFABOpen = false;
View fabBGLayout;
PlaceDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.places_layout);
viewModel = ViewModelProviders.of(this).get(PlacesViewModel.class);
Runnable r =new Runnable() {
#Override
public void run() {
recyclerView = findViewById(R.id.my_recycler_view);
placesAdapter = new PlacesAdapter(new ArrayList<PlaceSaved>());//, this);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
recyclerView.setAdapter(placesAdapter);
}
};
Thread newThread = new Thread(r);
newThread.start();
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Dialog
/*
Add location manually
*/
AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.place_add_dialog, null);
placeLLDialog.setView(view);
final EditText todo = view.findViewById(R.id.placeN);
final EditText time = view.findViewById(R.id.placell);
final EditText longi = view.findViewById(R.id.placell2);
placeLLDialog.setTitle("Add Place with Latitude and Longitude")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (!todo.getText().toString().equals("") &&
!time.getText().toString().equals("") &&
!longi.getText().toString().equals("")) {
Snackbar.make(view, "Running", Snackbar.LENGTH_LONG).show();
/* HERE I AM TRYING TO ADD THE DATA, WHICH IS NOT WORKING
final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
time.getText().toString(), longi.getText().toString());
AsyncTask.execute(new Runnable() {
#Override
public void run() {
db.databaseInterface().insertAll(placeSaved);
items = db.databaseInterface().getAllItems();
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter = new PlacesAdapter(items, db, null);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
closeFABMenu();
}
});
}
});*/
}
}
})
.setNegativeButton("Cancel", null);
AlertDialog alertDialog = placeLLDialog.create();
alertDialog.show();
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
});
since I am very new to java, I can't find out how to add the data to the database, which is inside fab1.setOnClickListner.
I will be grateful if someone kindly helps.
UPDATE
I forgot to add database itself, here it is:
#Database(entities = {PlaceSaved.class},version = 1)
public abstract class PlaceDatabase extends RoomDatabase {
private static PlaceDatabase INSTANCE;
public static PlaceDatabase getDatabase(Context context){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), PlaceDatabase.class,
"places_db").build();
}
return INSTANCE;
}
public abstract DatabaseInterface PlacedatabaseInterface();
Create Class AddBorrowViewModel
public class AddBorrowViewModel extends AndroidViewModel
{
private PlaceDatabase appDatabase;
public AddBorrowViewModel(#NonNull Application application) {
super(application);
appDatabase = PlaceDatabase.getDatabase(this.getApplication());
}
public void addBorrow(PlaceSaved placedSaved)
{
new addAsyncTask(appDatabase).execute(placedSaved);
}
private class addAsyncTask extends AsyncTask<PlaceSaved , Void, Void>
{
private PlaceDatabase appDatabase_;
addAsyncTask(PlaceDatabase appDatabase)
{
appDatabase_ = appDatabase;
}
#Override
protected Void doInBackground(PlaceSaved... placedSaved) {
appDatabase_.itemAndPersonModel().addBorrow(placedSaved[0]);
return null;
}
}
}
add this variable in your PlacesActivity
private AddBorrowViewModel addBorrowViewModel;
in OnCreate add this
addBorrowViewModel = ViewModelProviders.of(this).get(AddBorrowViewModel.class);
in onClickListener Of alertDilaog after snackbar
addBorrowViewModel.addBorrow(new PlaceSaved(
todo.getText().toString(),
time.getText().toString(), longi.getText().toString()
));
In your PlaceDatabase add
public abstract Dao itemAndPersonModel();

Categories

Resources