英文:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type (Model class)
问题
我努力尝试过,但我无法将类型为 java.lang.Long
的对象转换为类型(我的模型类)。
我在下面上传了我的 getDataFromFirebase()
方法:
MainActivity.class
private void getDataFromFirebase() {
binding.projectRecycler.setHasFixedSize(true);
binding.projectRecycler.setLayoutManager(new LinearLayoutManager(activity));
binding.projectRecycler.setAdapter(adapter);
adapter = new ProjectAdapter(activity, projectDetailsBeanList);
projectDetailsBeanList = new ArrayList<>();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseRef = firebaseDatabase.getReference("project_details");
DBListener = databaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
projectDetailsBeanList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ProjectModel.ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectModel.ProjectDetailsBean.class);
projectDetailsBean.setKey(snapshot.getKey());
projectDetailsBeanList.add(projectDetailsBean);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(activity, error.toString(), Toast.LENGTH_SHORT).show();
}
});
}
我的模型类
public class ProjectModel {
private ProjectDetailsBean project_details;
public ProjectDetailsBean getProject_details() {
return project_details;
}
public void setProject_details(ProjectDetailsBean project_details) {
this.project_details = project_details;
}
public static class ProjectDetailsBean {
private int architect_no;
private String client_name;
private String payment_received;
private String project_complete_date;
private String project_name;
private int project_no;
private String project_received_date;
private String key;
@Exclude
public String getKey() {
return key;
}
@Exclude
public void setKey(String key) {
this.key = key;
}
public int getArchitect_no() {
return architect_no;
}
public void setArchitect_no(int architect_no) {
this.architect_no = architect_no;
}
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
public String getPayment_received() {
return payment_received;
}
public void setPayment_received(String payment_received) {
this.payment_received = payment_received;
}
public String getProject_complete_date() {
return project_complete_date;
}
public void setProject_complete_date(String project_complete_date) {
this.project_complete_date = project_complete_date;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String project_name) {
this.project_name = project_name;
}
public int getProject_no() {
return project_no;
}
public void setProject_no(int project_no) {
this.project_no = project_no;
}
public String getProject_received_date() {
return project_received_date;
}
public void setProject_received_date(String project_received_date) {
this.project_received_date = project_received_date;
}
}
}
JSON 数据
{
"project_details": {
"architect_no": 1,
"client_name": "pratik bharad",
"payment_received": "yes",
"project_complete_date": "22/09/2020",
"project_name": "project of building",
"project_no": 1,
"project_received_date": "22/06/2020"
}
}
错误信息为:
com.google.firebase.database.DatabaseException: 无法将类型为 java.lang.Long 的对象转换为类型 com.psb.aurumdesign.ProjectModel$ProjectDetailsBean
英文:
I tried so hard but I Can't convert an object of type java.lang.Long
to type (My Model class).
I am uploading my getDataFromFirebase()
method below:
MainActivity.class
private void getDataFromFirebase() {
binding.projectRecycler.setHasFixedSize(true);
binding.projectRecycler.setLayoutManager(new LinearLayoutManager(activity));
binding.projectRecycler.setAdapter(adapter);
adapter = new ProjectAdapter(activity,projectDetailsBeanList);
projectDetailsBeanList = new ArrayList<>();
firebaseDatabase =FirebaseDatabase.getInstance();
databaseRef = firebaseDatabase.getReference("project_details");
DBListener = databaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
projectDetailsBeanList.clear();
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
ProjectModel.ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectModel.ProjectDetailsBean.class);
projectDetailsBean.setKey(snapshot.getKey());
projectDetailsBeanList.add(projectDetailsBean);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(activity, error.toString(), Toast.LENGTH_SHORT).show();
}
});
}
My Model Class
public class ProjectModel {
private ProjectDetailsBean project_details;
public ProjectDetailsBean getProject_details() {
return project_details;
}
public void setProject_details(ProjectDetailsBean project_details) {
this.project_details = project_details;
}
public static class ProjectDetailsBean {
private int architect_no;
private String client_name;
private String payment_received;
private String project_complete_date;
private String project_name;
private int project_no;
private String project_received_date;
private String key;
@Exclude
public String getKey() {
return key;
}
@Exclude
public void setKey(String key) {
this.key = key;
}
public int getArchitect_no() {
return architect_no;
}
public void setArchitect_no(int architect_no) {
this.architect_no = architect_no;
}
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
public String getPayment_received() {
return payment_received;
}
public void setPayment_received(String payment_received) {
this.payment_received = payment_received;
}
public String getProject_complete_date() {
return project_complete_date;
}
public void setProject_complete_date(String project_complete_date) {
this.project_complete_date = project_complete_date;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String project_name) {
this.project_name = project_name;
}
public int getProject_no() {
return project_no;
}
public void setProject_no(int project_no) {
this.project_no = project_no;
}
public String getProject_received_date() {
return project_received_date;
}
public void setProject_received_date(String project_received_date) {
this.project_received_date = project_received_date;
}
}
}
JSON data
{
"project_details" : {
"architect_no" : 1,
"client_name" : "pratik bharad",
"payment_received" : "yes",
"project_complete_date" : "22/09/2020",
"project_name" : "project of building",
"project_no" : 1,
"project_received_date" : "22/06/2020"
}
}
Error is:
> com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.psb.aurumdesign.ProjectModel$ProjectDetailsBean
答案1
得分: 1
你遇到了以下错误:
> com.google.firebase.database.DatabaseException: 无法将类型为java.lang.Long的对象转换为类型为com.psb.aurumdesign.ProjectModel$ProjectDetailsBean的对象
这是因为您尝试将project_details
节点下的第一个子项(类型为Long)转换为ProjectDetailsBean
类型的对象,实际上是不可能的。根据JSON模式,我看到在project_details
节点下实际上只有一个单一对象,因此无需循环遍历子项以获取数据。您可以直接将该对象映射到ProjectDetailsBean
类型的对象中,如下面的代码行所示:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference projectDetailsRef = rootRef.child("project_details");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectDetailsBean.class);
Log.d("TAG", projectDetailsBean.getProject_name());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //不要忽略潜在的错误!
}
};
projectDetailsRef.addListenerForSingleValueEvent(valueEventListener);
在Logcat中的结果将是项目的名称。如果您只需要单个属性的值,例如,您可以将以下代码行:
ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectDetailsBean.class);
更改为:
String projectName = dataSnapshot.child("project_name").getValue(String.class);
英文:
You are getting the following error:
> com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.psb.aurumdesign.ProjectModel$ProjectDetailsBean
Because you are trying to convert the first child from your project_details
node which is of type Long into an object of type ProjectDetailsBean
, which is actually not possible. As I see in the JSON schema, under the project_details
node, there is actually a single object, so there is no need to actually loop through the children in order to get the data. You can simply map that object directly into an object of type ProjectDetailsBean
, as in the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference projectDetailsRef = rootRef.child("project_details");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectDetailsBean.class);
Log.d("TAG", projectDetailsBean.getProject_name());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
}
};
projectDetailsRef.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat will be the name of the project. If you only need, for example, the value of a single property, you can change the following line of code:
ProjectDetailsBean projectDetailsBean = dataSnapshot.getValue(ProjectDetailsBean.class);
To:
String projectName = dataSnapshot.child("project_name").getValue(String.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论