简体   繁体   中英

No setter/getter Firebase

Hi everyone I have one question. I want to retrieve the data from the Firebase live database and it keeps telling me that it doesn't find the getter and the setter but as you can see below I did it, I create a class where I have the setter and the getter with the same name of the database fields.

Can anyone tell me what I'm doing wrong because I don't have a clue?

Thank you in advance.

Photo of the database:

在此处输入图片说明

Code of the Activity and the Class where you can see that I have the getter and the setter.

package com.example.ipill;

public class CartActivity extends AppCompatActivity {

    private TextView total;
    private ImageButton removeFromCart;
    private Button pay;


    private RecyclerView mResultList;
    private DatabaseReference mUserDatabase;
    public static int cart_count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        mUserDatabase = FirebaseDatabase.getInstance().getReference("Cart");

        total        = findViewById(R.id.TotalPrice);
        removeFromCart   = findViewById(R.id.removeFromCart);
        mResultList  = findViewById(R.id.cartList);
        pay    = findViewById(R.id.pay);


        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));



         // Attach a listener to read the data at our posts reference
          mUserDatabase.addValueEventListener(new ValueEventListener() {


            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Users_get post = dataSnapshot.getValue(Users_get.class);
                System.out.println("DATAAAA: "+post);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println("The read failed: " + databaseError.getCode());
            }
        });
    }



    @Override
    protected void onStart() {
        super.onStart();
        invalidateOptionsMenu();
    }

    // View Holder Class
    public static class UsersViewHolder extends RecyclerView.ViewHolder {
        View mView;
        String nome;
        String surname;
        Long prezzo;
        public UsersViewHolder(View itemView) {
            super(itemView);
            mView = itemView;

        }

        public void getDetails(String name,String cognome,Long price){
            nome=name;
            surname=cognome;
            prezzo=price;
        }


        public void setDetails(String name, String surname, Long price) {

            TextView user_name = (TextView) mView.findViewById(R.id.name_text);
            TextView user_surname = (TextView)mView.findViewById(R.id.status_text);
            TextView user_price = (TextView)mView.findViewById(R.id.price);

            user_name.setText(name);
            user_surname.setText(surname);
            user_price.setText(Long.toString(price));

        }
     }
}

CLASS

public class Users_get  {
    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    private String name, surname;
    private Long price;
}

JSON structure

{
  "Cart": {
    "-M0U3UXq2ZA00Jq5o9as": {
      "name": "Alex",
      "price": 120,
      "surname": "Kyto"
    },
    "-M0WlUbQHj1hdH40uWVF": {
      "name": "Alex",
      "price": 120,
      "surname": "Kyto"
    },
    "-M0WxZhI98Xb1s9Xy5HV": {
      "name": "Alex",
      "price": 120,
      "surname": "Kyto"
    },
    "-M0X00Zr64RocyQHhoFB": {
      "name": "Alex",
      "price": 120,
      "surname": "Kyto"
    }
  },
  "Users": {
    "01": {
      "Name": "Alex",
      "Price": 120,
      "Surname": "Kyto"
    },
    "02": {
      "Name": "Alex",
      "Price": 200,
      "Surname": "Pablo"
    }
  }
}

Change the class to this:

public class Users_get  {

private String name, surname;
private Long price;


public String getName() {
    return name;
}

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

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public Long getPrice() {
    return price;
}

public void setPrice(Long price) {
    this.price = price;
}


public Users_get(String name,Long price,String surname) {
    this.price = price;
    this.name = name;
    this.surname = surname;

}


public Users_get() {

}

Since you are using an IDE, then just click generate getters and setters and it will automatically do that for you instead of manually writing the methods..

You get the warning because in your Users_get class you don't have correct getters for your fields. If you have a field named name , the correct getter should be:

public String getName() {
    return name;
}

And not :

public String getname() {
    return name;
}

See the capital N versus lowercase n ? To solve this you should change all the names of all getters in your Users_get class, as explained above.

In addition to other answer, To get Users_get data, according your database structure you have to go one level more.

Currently your database reference pointed to Cart . So iterate through children of cart to get cart data. Check below:

mUserDatabase = FirebaseDatabase.getInstance().getReference("Cart");
mUserDatabase.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
            Users_get post = childSnapshot.getValue(Users_get.class);
            System.out.println("DATAAAA: " + post);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        System.out.println("The read failed: " + databaseError.getCode());
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM