this is the output I am getting after I changed names according to db
public class MainActivity extends AppCompatActivity {
private ListView coursesLV;
// creating a new array list.
ArrayList<Excel> coursesArrayList;
DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coursesLV = findViewById(R.id.idLVCourses);
// initializing our array list
coursesArrayList = new ArrayList<>();
getData();
}
private void getData() {
final ArrayAdapter<Excel> adapter = new ArrayAdapter<Excel>(this, android.R.layout.simple_dropdown_item_1line, coursesArrayList);
reference = FirebaseDatabase.getInstance().getReference().child("Excel");
reference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//myexcel = snapshot.getValue(Excel.class);
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
Excel currentUser = snapshot.getValue(Excel.class);
System.out.println("Age: " + currentUser.getAge());
System.out.println("Country: " + currentUser.getCountry());
System.out.println("Date: " + currentUser.getDate());
System.out.println("Name: " + currentUser.getFirst_Name());
System.out.println("Gender: " + currentUser.getGender());
System.out.println("ID: " + currentUser.getId());
System.out.println("Last Name: " + currentUser.getLast_Name());
System.out.println("Status: " + currentUser.getstatus());
coursesArrayList.add(currentUser);
adapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
adapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
coursesArrayList.remove(snapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
coursesLV.setAdapter(adapter);
}
this is my MainActivity.java class, in which I'm trying to fetch data from firebase realtime database and load it into listView. I'm able to see the output in logcat using println but data isn't appearing on android.
Here is my json in realtime db
{
"0": {
"Age": 32,
"Country": "United States",
"Date": "15/10/2017",
"First_Name": "Dulce",
"Gender": "Female",
"Id": 1562,
"Last_Name": "Abril",
"status": "open "
},
"1": {
"Age": 25,
"Country": "Great Britain",
"Date": "16/08/2016",
"First_Name": "Mara",
"Gender": "Female",
"Id": 1582,
"Last_Name": "Hashimoto",
"status": "close"
},
"2": {
"Age": 36,
"Country": "France",
"Date": "21/05/2015",
"First_Name": "Philip",
"Gender": "Male",
"Id": 2587,
"Last_Name": "Gent",
"status": "open "
},
"3": {
"Age": 25,
"Country": "United States",
"Date": "15/10/2017",
"First_Name": "Kathleen",
"Gender": "Female",
"Id": 3549,
"Last_Name": "Hanner",
"status": "close"
},
"4": {
"Age": 58,
"Country": "United States",
"Date": "16/08/2016",
"First_Name": "Nereida",
"Gender": "Female",
"Id": 2468,
"Last_Name": "Magwood",
"status": "open "
},
"5": {
"Age": 24,
"Country": "United States",
"Date": "21/05/2015",
"First_Name": "Gaston",
"Gender": "Male",
"Id": 2554,
"Last_Name": "Brumm",
"status": "close"
},
"6": {
"Age": 56,
"Country": "Great Britain",
"Date": "15/10/2017",
"First_Name": "Etta",
"Gender": "Female",
"Id": 3598,
"Last_Name": "Hurn",
"status": "open "
},
"7": {
"Age": 27,
"Country": "United States",
"Date": "16/08/2016",
"First_Name": "Earlean",
"Gender": "Female",
"Id": 2456,
"Last_Name": "Melgar",
"status": "open "
},
"8": {
"Age": 40,
"Country": "United States",
"Date": "21/05/2015",
"First_Name": "Vincenza",
"Gender": "Female",
"Id": 6548,
"Last_Name": "Weiland",
"status": "open "
},
this is my Excel class
public class Excel {
public Long Age;
public String Country;
public String Date;
public String First_Name;
public String Gender;
public Long Id;
public String Last_Name;
public String status;
public Excel() {
}
@PropertyName("Age")
public Long getAge() {
return Age;
}
@PropertyName("Country")
public String getCountry() {
return Country;
}
@PropertyName("Date")
public String getDate() {
return Date;
}
@PropertyName("First_Name")
public String getFirst_Name() {
return First_Name;
}
@PropertyName("Gender")
public String getGender() {
return Gender;
}
@PropertyName("Id")
public Long getId() {
return Id;
}
@PropertyName("Last_Name")
public String getLast_Name() {
return Last_Name;
}
@PropertyName("status")
public String getStatus() {
return status;
}
}
this is my activity_main.xml
<ListView 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"
android:orientation="horizontal"
android:id="@+id/idLVCourses"
tools:listitem="@layout/list_item"
tools:context=".MainActivity">
this my list_item
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="8dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/Age"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Age" />
<TextView
android:id="@+id/country"
android:layout_below="@id/Age"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Country" />
<TextView
android:id="@+id/date"
android:layout_below="@id/country"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Date" />
<TextView
android:id="@+id/Firstname"
android:layout_below="@id/date"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="First Name" />
<TextView
android:id="@+id/Gender"
android:layout_below="@id/Firstname"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Gender" />
<TextView
android:id="@+id/Id"
android:layout_below="@id/Gender"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Id" />
<TextView
android:id="@+id/LastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Id"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:text="Last Name"
android:textSize="20sp" />
<TextView
android:id="@+id/Status"
android:layout_below="@id/LastName"
android:layout_marginTop="10dp"
android:paddingStart="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Status" />