简体   繁体   English

使用Firebase适配器填充ListView

[英]Populate ListView with Firebase Adapter

I'm receiving these errors when I tried to populate a list view with firebase adapter using firebase UI 当我尝试使用firebase UI使用firebase适配器填充列表视图时,我收到了这些错误

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 com.google.firebase.database.DatabaseException:无法将java.util.HashMap类型的值转换为String com.google.firebase.database.DatabaseException:类java.util.HashMap具有泛型类型参数,请改用GenericTypeIndicator

Here Is the code 这是代码

     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);

                  }

here is an example of json data 这是json数据的一个例子

 "info" : {
   "KSTUJILdwPN305Fs7ujhga4knlG3" : {
     "File Info" : {
       "-JFRkA855rfOU7GtcK4" : {
         "Name" : "John",
         "Adress" : "Test Adress",
         "Favourite_food" : "Bread",
       },

info node does not refer to your data model. info节点不引用您的数据模型。 It may contain children nodes. 它可能包含子节点。 So to reach the model, you should use a reference like this. 因此,为了达到模型,您应该使用这样的引用。

 DatabaseReference a = ref.child("info").child(info_id).child("File Info").child(file_id); 

and you should have a FileInfo model instead of String model to use in 你应该有一个FileInfo模型而不是String模型

populateView(View v, FileInfo model, int position):

and model 和模型

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;
}

}
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 modelClass - Firebase会将某个位置的数据编组到您提供的类的实例中

replace the constructor's 2nd parameter (String.class) with a POJO with the fields from info (name, address, favouriteFood) 用POJO替换构造函数的第二个参数(String.class),其中包含来自info(name,address,favouriteFood)的字段

FirebaseListAdapter FirebaseListAdapter

Edit: 编辑:

You should also use getters in the populateView method 您还应该在populateView方法中使用getter

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

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