0

I am having a user account where each user account has its own investors

Below is my Investors class


public class Investor {

    int investorAvatar;
    String name;
    String username;
    String country;
    String phoneNo;
    String securityCode;
    String dateJoined;
    String sharedAgreed;

    public Investor() {
    }

    public Investor(String name, String username, String country,
                    String phoneNo, String securityCode,
                    String dateJoined, String sharedAgreed) {

        this.name = name;
        this.username = username;
        this.country = country;
        this.phoneNo = phoneNo;
        this.securityCode = securityCode;
        this.dateJoined = dateJoined;
        this.sharedAgreed = sharedAgreed;

    }

    public int getInvestorAvatar() {
        return investorAvatar;
    }

    public void setInvestorAvatar(int investorAvatar) {
        this.investorAvatar = investorAvatar;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDateJoined() {
        return dateJoined;
    }

    public void setDateJoined(String dateJoined) {
        this.dateJoined = dateJoined;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    public String getSecurityCode() {
        return securityCode;
    }

    public void setSecurityCode(String securityCode) {
        this.securityCode = securityCode;
    }

    public String getSharedAgreed() {
        return sharedAgreed;
    }

    public void setSharedAgreed(String sharedAgreed) {
        this.sharedAgreed = sharedAgreed;
    }
}

Below is how my my Investors are in Firebase Realtime Database

{
    "CVSmRGYF49b0D4QawhA7dF4ZSKT2": {
        "-Mn0hCk5sC7Og5_fyf0X": {
            "country": "Tanzania",
            "dateJoined": "2021-10-28",
            "investorAvatar": 0,
            "name": "usertest5",
            "phoneNo": "074561344",
            "securityCode": "8282829",
            "sharedAgreed": "12",
            "username": "testuser5"
        }
    },
    "DR2mU44n0CW91uB3AhB2W426WrK2": {
        "-Mn0awrOK1BDsCPnfH-Q": {
            "country": "kenya",
            "dateJoined": "2021-10-25",
            "investorAvatar": 0,
            "name": "Test User",
            "phoneNo": "0714810492",
            "securityCode": "154512122",
            "sharedAgreed": "12",
            "username": "testuser1"
        },
        "-Mn0ezofS6Rjt6KbaytX": {
            "country": "Uganda",
            "dateJoined": "2021-10-27",
            "investorAvatar": 0,
            "name": "testuser4",
            "phoneNo": "0763566445",
            "securityCode": "568956",
            "sharedAgreed": "12",
            "username": "user4"
        }
    }
}

CVSmRGYF49b0D4QawhA7dF4ZSKT2 and DR2mU44n0CW91uB3AhB2W426WrK2 are the user ids where below each user id there is a list of investors

Below is how i am fetching the list for investors in user id DR2mU44n0CW91uB3AhB2W426WrK2 only using an Investor Dao class and trying to print the list inside onViewCreated method in a fragment


public class InvestorDao {

    private final DatabaseReference rootDatabaseReference;

    private final FirebaseUser firebaseUser;

    public InvestorDao() {

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();


        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance("https://lucemtrader-default-rtdb.europe-west1.firebasedatabase.app");

        rootDatabaseReference = firebaseDatabase.getReference("Investors").child("DR2mU44n0CW91uB3AhB2W426WrK2");


    }


    public List<Investor> returnInvestorsList() {

        List<Investor> investorsList = new ArrayList<>();

        rootDatabaseReference.addValueEventListener(new ValueEventListener(){

            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot dataSnapshot: snapshot.getChildren()){

                    Investor investor = dataSnapshot.getValue(Investor.class);

                    investorsList.add(investor);

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        return investorsList;
    }


And below is how i am Logging my list inside onViewCreated method in fragment


Log.wtf("InvestorsList", new Gson().toJson(new InvestorDao().returnInvestorsList()));

But i am getting below error yet in my class i have already included the getters and setters what might be the problem


W/ClassMapper: No setter/field for CVSmRGYF49b0D4QawhA7dF4ZSKT2 found on class manu.apps.lucemtrader.classes.Investor
    No setter/field for DR2mU44n0CW91uB3AhB2W426WrK2 found on class manu.apps.lucemtrader.classes.Investor

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Emmanuel Njorodongo
  • 1,004
  • 17
  • 35
  • There is no way you can return the `investorsList` object as a result of the method. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Oct 31 '21 at 10:17
  • @Alex Mamo I was able to return it – Emmanuel Njorodongo Nov 03 '21 at 06:48

0 Answers0