我正在构建一个具有tabview的应用程序,其中包含两个不同的选项卡,用于丢失的项目和找到的项目。我使用Firebase作为我的数据库,并使用两个单独的FirebaseListAdapter填充列表视图。第一个选项卡中丢失项目的第一个列表视图加载并显示正常。当我尝试加载第二个标签时,我的应用程序崩溃并出现以下错误:
E / UncaughtException:com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为类型lostandfound.mi.ur.de.lostandfound.FoundItem
这些是我正在使用的一些代码片段: 的 MainActivity:
private void getFireBaseLostData() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference lostRef = ref.child("LostItem");
FirebaseListAdapter<LostItem> adapter = new FirebaseListAdapter<LostItem>(this, LostItem.class, android.R.layout.two_line_list_item, lostRef) {
@Override
protected void populateView(View v, LostItem model, int position) {
((TextView)v.findViewById(android.R.id.text1)).setText(model.getName());
((TextView)v.findViewById(android.R.id.text2)).setText(model.getDescription());
}
};
lostListView.setAdapter(adapter);
}
This is the corresponding item:
public class LostItem {
private String name;
private String date;
private double longitude;
private double latitude;
private String category;
private int id;
private String description;
private String town;
private String contact;
public LostItem(String name, String date, double longitude, double latitude, String category, String description, String town, String contact){
this.name=name;
this.date=date;
this.longitude = longitude;
this.latitude = latitude;
this.category=category;
this.description = description;
this.town = town;
this.contact = contact;
}
public LostItem(){
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setTown(String town) {
this.town = town;
}
public void setContact(String contact) {
this.contact = contact;
}
public void setDescription(String description){this.description = description;}
public String getDescription(){return description;}
public void setName(String name) {
this.name = name;
}
public void setDate(String date) {
this.date = date;
}
public void setCategory(String category) {
this.category = category;
}
public void setId(int id) {
this.id = id;
}
public String getName() {return name;}
public String getDate() {return date;}
public double getLongitude() {return longitude;}
public double getLatitude() {return longitude;}
public String getCategory() {return category;}
public String getContact() {return contact;}
public String getTown() {return town;}
public int getId() {return id;}
}
以下是崩溃我的应用程序的部分 - 它基本相同: MainActivity:
private void getFireBaseFoundData() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference foundRef = ref.child("FoundItem");
FirebaseListAdapter<FoundItem> foundItemFirebaseListAdapter = new FirebaseListAdapter<FoundItem>(this, FoundItem.class, android.R.layout.two_line_list_item, foundRef) {
@Override
protected void populateView(View v, FoundItem model, int position) {
((TextView)v.findViewById(android.R.id.text1)).setText(model.getName());
((TextView)v.findViewById(android.R.id.text2)).setText(model.getDescription());
}
};
foundListView.setAdapter(foundItemFirebaseListAdapter);
}
和FoundItem:
public class FoundItem {
private String name;
private String date;
private double longitude;
private double latitude;
private String category;
private int id;
private String description;
private String town;
private String contact;
public FoundItem(String name, String date, double longitude, double latitude, String category, String description, String town, String contact){
this.name=name;
this.date=date;
this.longitude = longitude;
this.latitude = latitude;
this.category=category;
this.description = description;
this.town = town;
this.contact = contact;
}
public FoundItem(){
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setTown(String town) {
this.town = town;
}
public void setContact(String contact) {
this.contact = contact;
}
public void setDescription(String description){this.description = description;}
public String getDescription(){return description;}
public void setName(String name) {
this.name = name;
}
public void setDate(String date) {
this.date = date;
}
public void setCategory(String category) {
this.category = category;
}
public void setId(int id) {
this.id = id;
}
public String getName() {return name;}
public String getDate() {return date;}
public double getLongitude() {return longitude;}
public double getLatitude() {return longitude;}
public String getCategory() {return category;}
public String getContact() {return contact;}
public String getTown() {return town;}
public int getId() {return id;}
}
JSON树:
{"FoundItem" : {
"-KSDsf6YVBBbSBHw7vID" : {
"category" : "Einen runden Hut",
"contact" : "Gar nicht ohne meinen Hut!",
"date" : "21.09.16",
"description" : "Es ist ein Hut",
"id" : 0,
"latitude" : 0.0,
"longitude" : 0.0,
"name" : "Hut",
"town" : "Regensburg"
}
},
"LostItem" : {
"-KSD_MKYWmLUbUyW0KYn" : {
"category" : "ein iphone suche ich",
"contact" : "gar nicht",
"date" : "21.09.16",
"description" : "gib mir mein iphone!",
"id" : 0,
"latitude" : 0.0,
"longitude" : 0.0,
"name" : "iphone",
"town" : "Regensburg"
}