本文介绍了如何使用java中的FirebaseListAdapter从firebase数据库读取儿童?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中很新。所以,我有这样的数据库结构:




$ $ $ $ $ $ c $ databaseReference = FirebaseDatabase.getInstance()。getReference()。child(Firebase)。child 用户);
firebaseListAdapter = new FirebaseListAdapter< String>(this,String.class,android.R.layout.simple_list_item_1,databaseReference){
@Override
protected void populateView(View v,String string,int位置){
TextView textView =(TextView)v.findViewById(android.R.id.text1);
textView.setText(string);
}
};
listView =(ListView)findViewById(R.id.listView);
listView.setAdapter(firebaseListAdapter);

这是我的User类:

  class User {
private String userName;
私人字符串电子邮件;

void setUserName(String userName){this.userName = userName;}
String getUserName(){return userName;}
$ b $ void setEmail(String email){ this.email = email;}
String getEmail(){return email;}
}

当我复制这个,我得到这个错误: com.google.firebase.database.DatabaseException:无法将类型java.util.HashMap的值转换为字符串



如何解决这个问题?



populateView 方法实现更改为



<$ p $ FirebaseListAdapter = new FirebaseListAdapter< User>(this,User.class,android.R.layout.simple_list_item_1,databaseReference){
@Override
protected void populateView(View v,用户用户,int位置){
TextView textView =(TextView)v.findViewById(android.R.id.text1);
字符串userKey = this.getRef(position).getKey();
textView.setText(userKey);
}
};


I am very new in Firebase. So, i have a database structure like this:

and want to display those generated id's into a listview. I'm using FirebaseListAdapter. Here is my code:

    databaseReference = FirebaseDatabase.getInstance().getReference().child("Firebase").child("Users");
    firebaseListAdapter = new FirebaseListAdapter<String>(this, String.class, android.R.layout.simple_list_item_1, databaseReference) {
        @Override
        protected void populateView(View v, String string, int position) {
            TextView textView = (TextView) v.findViewById(android.R.id.text1);
            textView.setText(string);
        }
    };
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(firebaseListAdapter);

This is my User class:

class User {
private String userName;
private String email;

    void setUserName(String userName) {this.userName = userName;}
    String getUserName() {return userName;}

    void setEmail(String email) {this.email = email;}
    String getEmail() {return email;}
}

When i copile this, i get this error: com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String

How can i fix this?

Thanks in advance!

解决方案

Change the populateView method implementation to

firebaseListAdapter = new FirebaseListAdapter<User>(this, User.class, android.R.layout.simple_list_item_1, databaseReference) {
    @Override
    protected void populateView(View v, User user, int position) {
        TextView textView = (TextView) v.findViewById(android.R.id.text1);
        String userKey = this.getRef(position).getKey();
        textView.setText(userKey);
    }
};

这篇关于如何使用java中的FirebaseListAdapter从firebase数据库读取儿童?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:28