我正在尝试从我的firebase数据库中检索数据并将其显示在ListView中的TextViews中。但是,我在这一行上遇到了“缺少构造函数”的异常:Clients clients = clientsSnapshot.getValue(Clients.class);
。
不确定如何修复它。我在firebase或android开发方面都不是很有经验,所以任何帮助都会非常感激。 干杯啦!
public class ViewProfile extends AppCompatActivity{
ListView listviewClients;
List<Clients> clientsList;
DatabaseReference databaseClients;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
listviewClients = (ListView) findViewById(R.id.listviewClients);
databaseClients = FirebaseDatabase.getInstance().getReference("clients");
clientsList = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
databaseClients.addValueEventListener (new ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
clientsList.clear();
for (com.google.firebase.database.DataSnapshot clientsSnapshot : dataSnapshot.getChildren()) {
Clients clients = clientsSnapshot.getValue(Clients.class);
clientsList.add(clients);
}
ClientsList adapter = new ClientsList(ViewProfile.this, clientsList);
listviewClients.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
Clients.java:
public class Clients {
String clientName;
String clientCareer;
Boolean techCB;
Boolean mediCB;
Boolean renewableEnergyNF;
Boolean googleNF;
Boolean novartisNF;
Boolean teslaNF;
Boolean fbLRS;
Boolean applLRS;
Boolean yhooLRS;
Boolean eurusdCB;
Boolean usdrubCU;
Boolean silverCO;
Boolean goldCO;
Boolean gbpusdCU;
Boolean nsdqI;
Boolean sp500I;
// public Clients(String id, String name, String career, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF, Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB, Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I) {
//
// }
public Clients(String id, String clientCareer, String clientName, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF,
Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB,
Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I) {
this.clientName = clientName;
this.clientCareer = clientCareer;
this.techCB = techCB;
this.mediCB = mediCB;
this.renewableEnergyNF = renewableEnergyNF;
this.googleNF = googleNF;
this.novartisNF = novartisNF;
this.teslaNF = teslaNF;
this.fbLRS = fbLRS;
this.applLRS = applLRS;
this.yhooLRS = yhooLRS;
this.eurusdCB = eurusdCB;
this.usdrubCU = usdrubCU;
this.silverCO = silverCO;
this.goldCO = goldCO;
this.gbpusdCU = gbpusdCU;
this.nsdqI = nsdqI;
this.sp500I = sp500I;
}
public String getClientName() {
return clientName;
}
public String getClientCareer() {
return clientCareer;
}
public Boolean getTechCB() {
return techCB;
}
public Boolean getMediCB() {
return mediCB;
}
public Boolean getRenewableEnergyNF() {
return renewableEnergyNF;
}
public Boolean getGoogleNF() {
return googleNF;
}
public Boolean getNovartisNF() {
return novartisNF;
}
public Boolean getTeslaNF() {
return teslaNF;
}
public Boolean getFbLRS() {
return fbLRS;
}
public Boolean getApplLRS() {
return applLRS;
}
public Boolean getYhooLRS() {
return yhooLRS;
}
public Boolean getEurusdCB() {
return eurusdCB;
}
public Boolean getUsdrubCU() {
return usdrubCU;
}
public Boolean getSilverCO() {
return silverCO;
}
public Boolean getGoldCO() {
return goldCO;
}
public Boolean getGbpusdCU() {
return gbpusdCU;
}
public Boolean getNsdqI() {
return nsdqI;
}
public Boolean getSp500I() {
return sp500I;
}
答案 0 :(得分:9)
在您的Model类中添加:无参数constructor
public class Clients {
private String name; //your variables
...
...
public Clients (){ } //no argument constructor
答案 1 :(得分:3)
我所知道的论点是他们喜欢参数
是的,参数和参数是一回事。
我不知道构造函数的定义,但它通常是公共_____(){}?
是的,这是正确的。错误消息告诉您,您的Clients
类没有没有任何参数的构造函数。如果创建没有构造函数的类,则会自动生成无参数构造函数。您收到错误是因为您有一个带有参数的Clients
构造函数。因此,您需要明确提供一个没有任何参数的文件,以使其与Firebase一起正常工作。
P.S。您应该使用boolean
代替Boolean
。有关更多详细信息,您应该了解有关基本类型和包装器对象之间差异的更多信息。