如何使用HashMap <string,string =“”>显示Firebase数据库中的记录?

时间:2017-03-16 00:58:20

标签: android firebase hashmap firebase-realtime-database listadapter

我正在开发一个用户可以拥有多个列表的应用。我的Firebase数据库如下所示:

enter image description here

我使用的模型是:

public class UserModel {
    private String userId;
    private String userName;
    private String userEmail;
    private HashMap<String, String> lists;

    public UserModel() {}

    public void setUserId(String userId) {this.userId = userId;}
    public String getUserId() {return userId;}

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

    public void setUserEmail(String userEmail) {this.userEmail = userEmail;}
    public String getUserEmail() {return userEmail;}

    public void setLists(HashMap<String, String> lists) {this.lists = lists;}
    public HashMap<String, String> getLists() {return lists;}
}

为了显示列表,我使用了FirebaseListAdapterListView。如果我使用这样的代码:

DatabaseReference listsRef = usersRef.child(userId).child("lists");
adapter = new FirebaseListAdapter<Map<String, String>>(MainActivity.this, Map.class, android.R.layout.simple_list_item_1, listsRef) {
    @Override
    protected void populateView(View v, Map<String, String> map, int position) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            Log.d(TAG, entry.getKey() + "/" + entry.getValue());
        }
    }                
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
}    

我收到此错误:

Error:(221, 31) error: no suitable constructor found for FirebaseListAdapter

我想要做的就是使用the key显示the valueHashMap。我哪里错了?提前谢谢!

1 个答案:

答案 0 :(得分:1)

populateView的第二个参数已经是列表中每个索引的值。因此,您只需将Map更改为String

adapter = new FirebaseListAdapter<String>(MainActivity.this, String.class, android.R.layout.simple_list_item_1, listsRef) {
    @Override
    protected void populateView(View v, String listName, int position) {
        String listKey = this.getRef(position).getKey();
        String listValue = listName;
    }
}    

希望这会有所帮助:)