使用 Firebase 适配器填充 ListView
Populate ListView with Firebase Adapter
当我尝试使用 firebase 使用 firebase 适配器填充列表视图时收到这些错误 UI
com.google.firebase.database.DatabaseException: Failed to convert
value of type java.util.HashMap to String
com.google.firebase.database.DatabaseException: Class
java.util.HashMap has generic type parameters, please use
GenericTypeIndicator instead
这是代码
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference a = ref.child("info");
final FirebaseListAdapter<String> adapter =
new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,a) {
@Override
protected void populateView(View v, String model, int position) {
TextView text = (TextView) findViewById(android.R.id.text1);
text.setText(model);
}
这里是json数据的例子
"info" : {
"KSTUJILdwPN305Fs7ujhga4knlG3" : {
"File Info" : {
"-JFRkA855rfOU7GtcK4" : {
"Name" : "John",
"Adress" : "Test Adress",
"Favourite_food" : "Bread",
},
final FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,a)
public FirebaseListAdapter(Activity activity,
java.lang.Class<T> modelClass,
int modelLayout,
com.firebase.client.Firebase ref)
modelClass - Firebase will marshall the data at a location into an instance of a class that you provide
将构造函数的第二个参数 (String.class) 替换为具有 info (name, address, favouriteFood) 字段的 POJO
编辑:
您还应该在 populateView 方法中使用 getter
信息节点未引用您的数据模型。它可能包含子节点。所以要达到模型,你应该使用这样的参考。
DatabaseReference a = ref.child("info").child(info_id).child("File Info").child(file_id);
你应该有一个 FileInfo 模型而不是 String 模型来使用
populateView(View v, FileInfo model, int position):
和型号
public class FileInfo {
private String Name;
private String Adress;
private String Favourite_food;
public FileInfo() {
}
public FileInfo(String Name, String Adress, String Favourite_food) {
this.Name = Name;
this.Adress = Adress;
this.Favourite_food = Favourite_food;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getAdress() {
return Adress;
}
public void setAdress(String Adress) {
this.Adress = Adress;
}
public String getFavourite_food() {
return Favourite_food;
}
public void setFavourite_food(String Favourite_food) {
this.Favourite_food = Favourite_food;
}
}
当我尝试使用 firebase 使用 firebase 适配器填充列表视图时收到这些错误 UI
com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String com.google.firebase.database.DatabaseException: Class java.util.HashMap has generic type parameters, please use GenericTypeIndicator instead
这是代码
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
DatabaseReference a = ref.child("info");
final FirebaseListAdapter<String> adapter =
new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,a) {
@Override
protected void populateView(View v, String model, int position) {
TextView text = (TextView) findViewById(android.R.id.text1);
text.setText(model);
}
这里是json数据的例子
"info" : {
"KSTUJILdwPN305Fs7ujhga4knlG3" : {
"File Info" : {
"-JFRkA855rfOU7GtcK4" : {
"Name" : "John",
"Adress" : "Test Adress",
"Favourite_food" : "Bread",
},
final FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,a)
public FirebaseListAdapter(Activity activity,
java.lang.Class<T> modelClass,
int modelLayout,
com.firebase.client.Firebase ref)
modelClass - Firebase will marshall the data at a location into an instance of a class that you provide
将构造函数的第二个参数 (String.class) 替换为具有 info (name, address, favouriteFood) 字段的 POJO
编辑:
您还应该在 populateView 方法中使用 getter
信息节点未引用您的数据模型。它可能包含子节点。所以要达到模型,你应该使用这样的参考。
DatabaseReference a = ref.child("info").child(info_id).child("File Info").child(file_id);
你应该有一个 FileInfo 模型而不是 String 模型来使用
populateView(View v, FileInfo model, int position):
和型号
public class FileInfo {
private String Name;
private String Adress;
private String Favourite_food;
public FileInfo() {
}
public FileInfo(String Name, String Adress, String Favourite_food) {
this.Name = Name;
this.Adress = Adress;
this.Favourite_food = Favourite_food;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getAdress() {
return Adress;
}
public void setAdress(String Adress) {
this.Adress = Adress;
}
public String getFavourite_food() {
return Favourite_food;
}
public void setFavourite_food(String Favourite_food) {
this.Favourite_food = Favourite_food;
}
}