简体   繁体   English

没有 setter/getter Firebase

[英]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.我想从 Firebase 实时数据库中检索数据,它一直告诉我它没有找到 getter 和 setter,但是正如您在下面看到的,我做到了,我创建了一个类,其中有 setter 和 getter同名的数据库字段。

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.活动和类的代码,您可以在其中看到我有 getter 和 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 JSON 结构

{
  "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..由于您使用的是 IDE,因此只需单击生成 getter 和 setter,它就会自动为您执行此操作,而不是手动编写方法。

You get the warning because in your Users_get class you don't have correct getters for your fields.您收到警告是因为在您的Users_get类中,您的字段没有正确的 getter。 If you have a field named name , the correct getter should be:如果您有一个名为name的字段,则正确的 getter 应该是:

public String getName() {
    return name;
}

And not :不是

public String getname() {
    return name;
}

See the capital N versus lowercase n ?看到大写N与小写n吗? To solve this you should change all the names of all getters in your Users_get class, as explained above.要解决这个问题,您应该更改Users_get类中所有 getter 的所有名称,如上所述。

In addition to other answer, To get Users_get data, according your database structure you have to go one level more.除了其他答案,要获取Users_get数据,根据您的数据库结构,您必须再上一级。

Currently your database reference pointed to Cart .目前您的数据库引用指向Cart So iterate through children of cart to get cart data.因此,遍历cart 的子代以获取cart 数据。 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());
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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