Firebase Android ListView not being displayed
I am trying to display my data from firebase real time database on a listview in the main menu screen once the users log in. The app is running but its not displaying the data.
This is the data in my database
Now for the codes.
MainMenu.java This function is called on the OnCreate().
public void makeItem ()
{
lv = findViewById(R.id.listView);
db = FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(db);
adapter= new AdapterItem(this,helper.retrive());
lv.setAdapter(adapter);
}
CustomListAdapter.java
public class CustomListAdapter{
private String ItemName;
private String Quantity;
private String SerialNo;
private String SupplierName;
private String SupplierEmail;
private String SupplierPhone;
public CustomListAdapter(){
}
public CustomListAdapter (String ItemName,String Quantity,String SerialNo,String SupplierName,String SupplierEmail,String SupplierPhone)
{
this.ItemName = ItemName;
this.Quantity = Quantity;
this.SerialNo = SerialNo;
this.SupplierName = SupplierName;
this.SupplierEmail = SupplierEmail;
this.SupplierPhone = SupplierPhone;
}
public void setItemName (String ItemName)
{
this.ItemName = ItemName;
}
public String getItemName ()
{
return ItemName;
}
public void setQuantity (String Quantity)
{
this.Quantity = Quantity;
}
public String getQuantity ()
{
return Quantity;
}
public void setSerialNo (String SerialNo)
{
this.SerialNo = SerialNo;
}
public String getSerialNo ()
{
return SerialNo;
}
public void setSupplierName (String SupplierName)
{
this.SupplierName = SupplierName;
}
public String getSupplierName()
{
return SupplierName;
}
public void setSupplierEmail (String SupplierEmail)
{
this.SupplierEmail = SupplierEmail;
}
public String getSupplierEmail() {
return SupplierEmail;
}
public void setSupplierPhone (String SupplierPhone)
{
this.SupplierPhone = SupplierPhone;
}
public String getSupplierPhone() {
return SupplierPhone;
}
}
AdapterItem.java
public class AdapterItem extends BaseAdapter {
Context c;
ArrayList<CustomListAdapter> customListAdapters;
public AdapterItem(Context c, ArrayList<CustomListAdapter> customListAdapters) {
this.c = c;
this.customListAdapters = customListAdapters;
}
@Override
public int getCount() {
return customListAdapters.size();
}
@Override
public Object getItem(int position) {
return customListAdapters.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=LayoutInflater.from(c).inflate(R.layout.content_main_menu_list,parent,false);
}
TextView ItemName = convertView.findViewById(R.id.name);
TextView SerialNo = convertView.findViewById(R.id.serialNo);
TextView SupplierName = convertView.findViewById(R.id.supplierName);
TextView amount = convertView.findViewById(R.id.amount);
final CustomListAdapter CLA = (CustomListAdapter) this.getItem(position);
ItemName.setText(CLA.getItemName());
SerialNo.setText(CLA.getSerialNo());
SupplierName.setText(CLA.getSupplierName());
amount.setText(CLA.getQuantity());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c,CLA.getItemName(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
content_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main_menu">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp" />
content_main_menu_list.xml is a custom layout that i created for every data set to be displayed.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/serialNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/supplierName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="@+id/serialNo" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginBottom="8dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0171B0"
app:layout_constraintBottom_toTopOf="@+id/serialNo" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
The problem in your code lies in the fact that you have in your CustomListAdapter
class a field named ItemName
but you are using a getter named getItemName()
, which is not correct since Firebase is looking in the database for a field named itemName
and not ItemName
. See the lowercase i
letter vs. capital letter I
?
There are two ways in which you can solve this problem. The first one would be to change your model class by renaming the fields according to the Java Naming Conventions. So you model class should look like this:
public class CustomListAdapter {
private String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
public CustomListAdapter() {}
public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
this.itemName = itemName;
this.quantity = quantity;
this.serialNo = serialNo;
this.supplierName = supplierName;
this.supplierEmail = supplierEmail;
this.supplierPhone = supplierPhone;
}
public String getItemName() { return itemName; }
public String getQuantity() { return quantity; }
public String getSerialNo() { return serialNo; }
public String getSupplierName() { return supplierName; }
public String getSupplierEmail() { return supplierEmail; }
public String getSupplierPhone() { return supplierPhone; }
}
See in this example, there are private
fields and public getters. There is also a simpler solution, to set the value directly on public fields like this:
public class CustomListAdapter {
public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
}
Now just remove the current data and add it again using the correct names. This solution will work only if you are in testing phase.
There is also the second approach, which is to use annotations
. So if you prefer to use private fields and public getters, you should use the PropertyName annotation only in front of the getter. So your CustomListAdapter
class should look like this:
public class CustomListAdapter {
private String ItemName;
private String Quantity;
private String SerialNo;
private String SupplierName;
private String SupplierEmail;
private String SupplierPhone;
public CustomListAdapter() {}
public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
ItemName = itemName;
Quantity = quantity;
SerialNo = serialNo;
SupplierName = supplierName;
SupplierEmail = supplierEmail;
SupplierPhone = supplierPhone;
}
@PropertyName("ItemName")
public String getItemName() { return ItemName; }
@PropertyName("Quantity")
public String getQuantity() { return Quantity; }
@PropertyName("SerialNo")
public String getSerialNo() { return SerialNo; }
@PropertyName("SupplierName")
public String getSupplierName() { return SupplierName; }
@PropertyName("SupplierEmail")
public String getSupplierEmail() { return SupplierEmail; }
@PropertyName("SupplierPhone")
public String getSupplierPhone() { return SupplierPhone; }
}