0

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" />
  • Please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Aug 08 '22 at 09:56
  • Thank you for your reply, I added my Database structure. this data is showing in my Logcat as a list but my screen is blank – Saloni Tyagi Aug 08 '22 at 10:33
  • Please also edit your question and add the content of your `Excel` class. – Alex Mamo Aug 08 '22 at 10:38
  • okay I added Content of Excel class – Saloni Tyagi Aug 08 '22 at 10:47
  • I tried suggested code but the is still not showing on my screen – Saloni Tyagi Aug 08 '22 at 12:18
  • @AlexMamo While the links you provided seem related, it's probably not enough for OP to figure out to apply them to their problem. Can you also leave a comment explaining what is going wrong, and how they need to apply the information from the link to their problem? – Frank van Puffelen Aug 08 '22 at 14:48
  • @SaloniTyagi The problem in your code lies in the fact that the names of the fields inside the class are different than the ones that exist in the database. So please check the duplicate answers, to see how you can solve this issue. In your case, using the annotation will be the simplest solution. – Alex Mamo Aug 08 '22 at 14:53
  • @FrankvanPuffelen Sure, I did before, but most likely I forget the hit enter. Thanks. – Alex Mamo Aug 08 '22 at 14:54
  • @AlexMamo thank you I changed the names according to database now I am getting package name with random numbers on the screen in list View .. – Saloni Tyagi Aug 09 '22 at 05:29
  • @AlexMamo Sir I added a Screenshot of my output and My xml code as well please check that – Saloni Tyagi Aug 09 '22 at 06:08
  • Those are the HashCode of the objects. Implement toString or get the data out of the objects. – Alex Mamo Aug 09 '22 at 07:39
  • 1
    Thank you @AlexMamo implementing toString worked for me – Saloni Tyagi Aug 18 '22 at 07:17

0 Answers0