I am new in respect to fetching data from Firebase Realtime Database in an Android app. Till now I have just found the code on the internet and changed it as per my requirement. I have created my realtime database in Firebase and also connected the same to my android app. So, In my android application I have a navigation view which navigates to 4 different Fragments inside mainActivity.kt file. I have created 2 fragments in java, hope that is not a issue here?
So, item fragment here is fetching the data from firebase and note fragment here is sending notes to firebase realtime database. I am using a recycler view inside item fragment. I get no error messages but I am unable to view the data in the item fragment and when I try to send notes inside note fragment the app crashes. I am not able to figure out the reason for this. Can anyone help?
Here is my code for item fragment:
package com.MechaProjectS6.whatisinmyfridge.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.MechaProjectS6.whatisinmyfridge.R;
import com.MechaProjectS6.whatisinmyfridge.adapter.myadapter;
import com.MechaProjectS6.whatisinmyfridge.model.model;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
public class ItemFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
RecyclerView recyclerView;
myadapter adapter;
public ItemFragment() {
}
public static ItemFragment newInstance(String param1, String param2) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_item, container, false);
recyclerView = (view.findViewById(R.id.recycler));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
FirebaseRecyclerOptions<model> options =
new FirebaseRecyclerOptions.Builder<model>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("items"), model.class)
.build();
adapter = new myadapter(options);
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
Here is the model class
package com.MechaProjectS6.whatisinmyfridge.model;
public class model {
String Item, TimeStamp, Weight;
public model() {
}
public model(String item, String timeStamp, String weight) {
Item = item;
TimeStamp = timeStamp;
Weight = weight;
}
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public String getTimeStamp() {
return TimeStamp;
}
public void setTimeStamp(String timeStamp) {
TimeStamp = timeStamp;
}
public String getWeight() {
return Weight;
}
public void setWeight(String weight) {
Weight = weight;
}
}
Here is the adapter file
package com.MechaProjectS6.whatisinmyfridge.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.MechaProjectS6.whatisinmyfridge.R;
import com.MechaProjectS6.whatisinmyfridge.model.model;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class myadapter extends FirebaseRecyclerAdapter<model, myadapter.myViewHolder> {
public myadapter(@NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull model model) {
holder.txtTimeStamp.setText(model.getTimeStamp());
holder.txtItemName.setText(model.getItem());
holder.txtItemWeight.setText(model.getWeight());
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_dashboard_single_row, parent, false);
return new myViewHolder(view);
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView txtTimeStamp, txtItemName, txtItemWeight;
public myViewHolder(@NonNull View itemView) {
super(itemView);
txtTimeStamp = itemView.findViewById(R.id.txtTimeStamp);
txtItemName = itemView.findViewById(R.id.txtItemName);
txtItemWeight = itemView.findViewById(R.id.txtItemWeight);
}
}
}
Here is the Note Fragment
package com.MechaProjectS6.whatisinmyfridge.fragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.MechaProjectS6.whatisinmyfridge.R;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class NoteFragment extends Fragment {
EditText e1;
Button b1;
DatabaseReference databaseReference;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public NoteFragment() {
// Required empty public constructor
}
public static NoteFragment newInstance(String param1, String param2) {
NoteFragment fragment = new NoteFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
e1 = view.findViewById(R.id.add_notes);
b1 = view.findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String fruitName = e1.getText().toString().trim();
if (!fruitName.isEmpty()) {
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR);
String time = hour + ":" + minutes + ":" + seconds;
int day = c.get(Calendar.DAY_OF_MONTH);
int month = c.get(Calendar.MONTH);
int year = c.get(Calendar.YEAR);
String date = day + "/" + month + "/" + year;
String DateTimes = "Date: " + date + " Time " + time;
DateFormat df = new SimpleDateFormat("dd-MM-yy:HH:mm:ss");
Date dateobj = new Date();
String d = df.format(dateobj);
databaseReference = FirebaseDatabase.getInstance().getReference("Notes");
databaseReference.child("DateTime:" + d).setValue(fruitName);
Toast.makeText(getActivity(), "Saved Successfully", Toast.LENGTH_LONG).show();
e1.setText("");
} else {
Toast.makeText(getActivity(), "Please enter Fruit name and Weight", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
}
Here I am also adding the xml files For item Fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.ItemFragment">
<TextView
android:id="@+id/txtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "@string/items_in_fridge" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below = "@+id/txtItemName"
android:padding="10dp"
android:layout_margin="5dp"/>
</RelativeLayout>
Single Row for Recycler view :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3.3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<TextView
android:id="@+id/txtTimeStamp"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Time Stamp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<TextView
android:id="@+id/txtItemName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/txtTimeStamp"
android:text="List Item"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<TextView
android:id="@+id/txtItemWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/txtItemName"
android:text="200g"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:padding="10dp"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
For notes Fragment :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.NoteFragment">
<EditText
android:id="@+id/editAddNotes"
android:layout_width="match_parent"
android:layout_height="250dp"
android:hint="Add Notes"
android:layout_margin="10dp"
android:padding="10dp"
android:textIsSelectable="true"
android:textColor="@color/colorPrimary"
android:textSize="20sp" />
<Button
android:id="@+id/button"
android:layout_below="@+id/editAddNotes"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimaryDark"
android:text="Save"
android:textSize="30sp"
android:textStyle="bold"/>
</RelativeLayout>