I am new to android and I couldn't find why the app keeps on crashing whenever I click the admin option from the drawer menu, the recycler view is not at all getting displayed
AdminBookDetails.java
public class AdminBookDetails extends AppCompatActivity implements
purchaseadapter.PurchaseClickInterface{
private RecyclerView booksRV;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private ConstraintLayout purchaseCL;
private purchaseadapter purchaseAdapter;
private Button Editbookdetails;
private RelativeLayout RLbottomsheet;
private ArrayList<purchasemodel> purchasemodelArrayList;
private FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_book_details);
getSupportActionBar().setTitle("Admin Book Details");
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
booksRV = findViewById(R.id.idRVpurchasebooks);
Editbookdetails = findViewById(R.id.Editdetails);
firebaseDatabase = FirebaseDatabase.getInstance();
fAuth = FirebaseAuth.getInstance();
databaseReference = firebaseDatabase.getReference("books");
Editbookdetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AdminBookDetails.this, admin_editbookdetails.class);
startActivity(i);
}
});
purchasemodelArrayList = new ArrayList<purchasemodel>();
//purchaseCL = findViewById(R.id.purchasebooksCL);
RLbottomsheet = findViewById(R.id.design_bottom_sheet);
// we are initializing our adapter class and passing our arraylist to it.
purchaseAdapter = new purchaseadapter(purchasemodelArrayList,this,this);
System.out.println("\n\narray passed\n\n");
// below line is for setting a layout manager for our recycler view.
// here we are creating vertical list so we will provide orientation as vertical
LinearLayoutManager linearLayoutManage = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
// in below two lines we are setting layoutmanager and adapter to our recycler view.
booksRV.setLayoutManager(linearLayoutManage);
booksRV.setAdapter(purchaseAdapter);
getAllBooks();
}
private void getAllBooks(){
purchasemodelArrayList.clear();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
purchasemodelArrayList.add(snapshot.getValue(purchasemodel.class));
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});}
@Override
public void onCourseClick(int position) {
displaybottomsheetdialog(purchasemodelArrayList.get(position));
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
// adding a click listener for option selected on below line.
int id = item.getItemId();
switch (id) {
case R.id.idLogOut:
// displaying a toast message on user logged out inside on click.
Toast.makeText(getApplicationContext(), "User Logged Out", Toast.LENGTH_LONG).show();
// on below line we are signing out our user.
fAuth.signOut();
// on below line we are opening our login activity.
Intent i = new Intent(AdminBookDetails.this, Login.class);
startActivity(i);
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// on below line we are inflating our menu
// file for displaying our menu options.
getMenuInflater().inflate(R.menu.drawer_menu, menu);
return true;
}
private void displaybottomsheetdialog(purchasemodel Purchasemodel){
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
View layout = LayoutInflater.from(this).inflate(R.layout.bottomsheet, purchaseCL);
// setting content view for bottom sheet on below line.
bottomSheetDialog.setContentView(layout);
// on below line we are setting a cancelable
bottomSheetDialog.setCancelable(false);
bottomSheetDialog.setCanceledOnTouchOutside(true);
// calling a method to display our bottom sheet.
bottomSheetDialog.show();
TextView Booktitle = layout.findViewById(R.id.TVBooktitle);
TextView Author = layout.findViewById(R.id.TVauthor);
ImageView BookImg = layout.findViewById(R.id.IVbook);
TextView Publisher = layout.findViewById(R.id.TVPublisher);
Button Editbutton = layout.findViewById(R.id.BtnEdt);
Button Viewbutton = layout.findViewById(R.id.BtnView);
Booktitle.setText(Purchasemodel.getBook_title());
Author.setText(Purchasemodel.getAuthor_name());
//BookImg.setText(Purchasemodel.getBook_image());
Publisher.setText(Purchasemodel.getpublisher());
Picasso.get().load(Purchasemodel.getBook_image()).into(BookImg);
Editbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AdminBookDetails.this,admin_editbookdetails.class);
i.putExtra("books",Purchasemodel);
startActivity(i);
}
});
}
}
activity_admin_edit_book_details.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".admin_editbookdetails">
<EditText
android:id="@+id/ETbooktitle"
android:layout_width="380dp"
android:layout_height="53dp"
android:background="@drawable/rounded_blue_border"
android:backgroundTint="#F5F8F9"
android:ems="10"
android:hint="Fluid Mechanics Volume 1"
android:inputType="text"
android:textColorHighlight="#E53935"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.147" />
<EditText
android:id="@+id/ETAuthor"
android:layout_width="380dp"
android:layout_height="53dp"
android:background="@drawable/rounded_blue_border"
android:backgroundTint="#F5F8F9"
android:ems="10"
android:hint="Preethi Shenoy"
android:inputType="text"
android:textColorHighlight="#E53935"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.368"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.284" />
<TextView
android:id="@+id/TVauthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter_medium"
android:text="Author Name"
android:textColor="#040303"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.029"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
<TextView
android:id="@+id/TVcatogery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter_medium"
android:text="Book image"
android:textColor="#040303"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.52" />
<EditText
android:id="@+id/ETbookimage"
android:layout_width="380dp"
android:layout_height="53dp"
android:background="@drawable/rounded_blue_border"
android:backgroundTint="#F5F8F9"
android:ems="10"
android:hint=" Enter image URL"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.322"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.573" />
<EditText
android:id="@+id/ETpublisher"
android:layout_width="380dp"
android:layout_height="53dp"
android:background="@drawable/rounded_blue_border"
android:backgroundTint="#F5F8F9"
android:ems="10"
android:hint="New Age international Publishers"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.328"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.428" />
<TextView
android:id="@+id/TVPublisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter_medium"
android:text="Publisher Name"
android:textColor="#040303"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.045"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.38" />
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="Book Details"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#050505"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.042"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="Delete\n Book Details"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.924" />
<Button
android:id="@+id/Editbookdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="Edit \n Book Details"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.884"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.923" />
</androidx.constraintlayout.widget.ConstraintLayout>
admin_editbookdetails.java
package com.example.books;
public class admin_editbookdetails extends AppCompatActivity {
EditText ETBookTitle;
EditText ETAuthor;
EditText ETPublisher;
EditText ETBookImgUrl;
Button EditBookdetailsbutton;
Button DeleteBookDetailsbutton;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
String BookID;
private purchasemodel Purchasemodel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_edit_book_details);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Book Details");
ETBookTitle = findViewById(R.id.ETbooktitle);
ETAuthor = findViewById(R.id.ETAuthor);
ETPublisher = findViewById(R.id.ETpublisher);
ETBookImgUrl = findViewById(R.id.ETbookimage);
EditBookdetailsbutton = findViewById(R.id.Editbookdetails);
DeleteBookDetailsbutton = findViewById(R.id.button4);
firebaseDatabase = FirebaseDatabase.getInstance();
Purchasemodel = getIntent().getParcelableExtra("books");
if(Purchasemodel!= null ) {
ETBookTitle.setText(Purchasemodel.getBook_title());
ETAuthor.setText(Purchasemodel.getAuthor_name());
ETPublisher.setText(Purchasemodel.getpublisher());
ETBookImgUrl.setText(Purchasemodel.getBook_image());
BookID = Purchasemodel.getBookId();
}
databaseReference = firebaseDatabase.getReference("books").child(BookID);
EditBookdetailsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String BookTitle = ETBookTitle.getText().toString();
String Author = ETAuthor.getText().toString();
String BookImgUrl = ETBookImgUrl.getText().toString();
String Publisher = ETPublisher.getText().toString();
Map<String, Object> map = new HashMap<>();
map.put("book_title", BookTitle);
map.put("author_name", Author);
map.put("book_image", BookImgUrl);
map.put("publisher", Publisher);
map.put("bookId",BookID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
databaseReference.child(BookID).setValue(Purchasemodel);
databaseReference.updateChildren(map);
// on below line we are displaying a toast message.
Toast.makeText(admin_editbookdetails.this, "Book Details Updated..",
Toast.LENGTH_SHORT).show();
startActivity(new Intent(admin_editbookdetails.this,
AdminBookDetails.class));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(admin_editbookdetails.this, "Fail to update book
details..", Toast.LENGTH_SHORT).show();
}
});
}
});
// adding a click listener for our delete course button.
DeleteBookDetailsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling a method to delete a course.
deleteCourse();
}
private void deleteCourse() {
databaseReference.removeValue();
// displaying a toast message on below line.
Toast.makeText(admin_editbookdetails.this, "Book Info Deleted..", Toast.LENGTH_SHORT).show();
// opening a main activity on below line.
startActivity(new Intent(admin_editbookdetails.this, AdminBookDetails.class));
}
});
}
}
Purchasebooks.java
public class Purchasebooks extends AppCompatActivity implements
purchaseadapter.PurchaseClickInterface {
private RecyclerView booksRV;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
private ConstraintLayout bottomsheetCL;
private RelativeLayout RLbottomsheet;
private purchaseadapter purchaseAdapter;
private Button Addbookdetails;
private ArrayList<purchasemodel> purchasemodelArrayList;
//DatabaseReference purchaseDbref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchasebooks);
getSupportActionBar().setTitle("Purchase books");
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
booksRV = findViewById(R.id.idRVpurchasebooks);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("books");
//purchaseDbref = FirebaseDatabase.getInstance().getReference().child("Books");
purchasemodelArrayList = new ArrayList<purchasemodel>();
// bottomsheetCL = findViewById(R.id.purchasebooksCL);
RLbottomsheet = findViewById(R.id.design_bottom_sheet);
// we are initializing our adapter class and passing our arraylist to it.
purchaseAdapter = new purchaseadapter(purchasemodelArrayList,this,this);
System.out.println("\n\narray passed\n\n");
// below line is for setting a layout manager for our recycler view.
// here we are creating vertical list so we will provide orientation as vertical
LinearLayoutManager linearLayoutManage = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
// in below two lines we are setting layoutmanager and adapter to our recycler view.
booksRV.setLayoutManager(linearLayoutManage);
booksRV.setAdapter(purchaseAdapter);
Addbookdetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Purchasebooks.this,admin_enterbookdetails.class));
}
});
getAllBooks();
}
private void getAllBooks(){
purchasemodelArrayList.clear();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String
previousChildName) {
purchasemodelArrayList.add(snapshot.getValue(purchasemodel.class));
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String
previousChildName) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String
previousChildName) {
purchaseAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});}
@Override
public void onCourseClick(int position) {
}
}
purchasemodel.java
package com.example.books;
import android.os.Parcel;
import android.os.Parcelable;
public class purchasemodel implements Parcelable {
private String book_title;
private String author_name;
private String publisher;
private String book_image;
private String cartbutton;
private String bookId;
public purchasemodel(){
}
// Constructor
public purchasemodel(String book_title, String author_name,String publisher, String
book_image, String bookID) {
this.book_title = book_title;
this.author_name = author_name;
this.book_image = book_image;
this.publisher = publisher;
this.bookId = bookID;
}
protected purchasemodel(Parcel in) {
book_title = in.readString();
author_name = in.readString();
publisher = in.readString();
book_image = in.readString();
cartbutton = in.readString();
bookId = in.readString();
}
public static final Creator<purchasemodel> CREATOR = new Creator<purchasemodel>() {
@Override
public purchasemodel createFromParcel(Parcel in) {
return new purchasemodel(in);
}
@Override
public purchasemodel[] newArray(int size) {
return new purchasemodel[size];
}
};
// Getter and Setter
public String getpublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getcartbutton() {
return cartbutton;
}
public void setCartbutton(String cartbutton) {
this.cartbutton = cartbutton;
}
public String getBook_title() {
return book_title;
}
public void setBook_title(String book_title) {
this.book_title = book_title;
}
public void setAuthor_name(String author_name) {
this.author_name = author_name;
}
public String getAuthor_name() {
this.author_name = author_name;
return author_name;
}
public String getBookId() {
this.bookId = bookId;
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBook_image() {
return book_image;
}
public void setBook_image(String book_image) {
this.book_image = book_image;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(book_title);
dest.writeString(author_name);
dest.writeString(publisher);
dest.writeString(book_image);
dest.writeString(cartbutton);
dest.writeString(bookId);
}
}
purchaseadapter.java
package com.example.books;
public class purchaseadapter extends RecyclerView.Adapter<purchaseadapter.Viewholder> {
private ArrayList<purchasemodel> purchasemodelArrayList;
private Context context;
private PurchaseClickInterface purchaseClickInterface;
int lastpos = -1;
public purchaseadapter(ArrayList<purchasemodel> purchasemodelArrayList,Context context,
PurchaseClickInterface purchaseClickInterface) {
this.context = context;
this.purchasemodelArrayList = purchasemodelArrayList;
this.purchaseClickInterface = purchaseClickInterface;
}
@NonNull
@Override
public purchaseadapter.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
// to inflate the layout for each item of recycler view.
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout_purchasebooks,
parent, false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull purchaseadapter.Viewholder holder,
@SuppressLint("RecyclerView") int position) {
purchasemodel purchasemodel = purchasemodelArrayList.get(position);
holder.booktitleTV.setText(purchasemodel.getBook_title());
holder.publisherTV.setText(purchasemodel.getpublisher());
holder.authorTV.setText(purchasemodel.getAuthor_name());
holder.cartbutton.setText(purchasemodel.getcartbutton());
Picasso.get().load(purchasemodel.getBook_image()).into(holder.bookIV);
setAnimation(holder.itemView,position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
purchaseClickInterface.onCourseClick(position);
}
});
}
private void setAnimation(View itemView, int position){
if(position>lastpos){
Animation animation = AnimationUtils.loadAnimation(context,
android.R.anim.slide_in_left);
itemView.setAnimation(animation);
lastpos=position;
}
}
@Override
public int getItemCount() {
// this method is used for showing number
// of card items in recycler view.
return purchasemodelArrayList.size();
}
public interface PurchaseClickInterface {
void onCourseClick(int position);
}
// View holder class for initializing of
// your views such as TextView and Imageview.
public class Viewholder extends RecyclerView.ViewHolder {
private ImageView bookIV;
private TextView booktitleTV, authorTV, publisherTV;
private Button cartbutton;
public Viewholder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(v.getContext(), Detailspage.class);
}
});
bookIV = itemView.findViewById(R.id.idIVbookImage);
booktitleTV = itemView.findViewById(R.id.idTVbooktitle);
authorTV = itemView.findViewById(R.id.idTVauthor);
publisherTV = itemView.findViewById(R.id.idTVpublisher);
cartbutton = itemView.findViewById(R.id.buttoncart);
}
}
}