Alex Mamo
Alex Mamo

Reputation: 138824

How to display records from a Firebase database using HashMap<String, String>?

I am developing an app in which the users can have multiple lists. My Firebase database looks like this:

enter image description here

The model that i use is this:

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

For displaying the lists, i use a FirebaseListAdapter and a ListView. If i use the code like this:

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

I get this error:

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

All I want to do is to display the key and the value using HashMap. Where am I wrong? Thanks in advance!

Upvotes: 0

Views: 453

Answers (1)

Wilik
Wilik

Reputation: 7720

The second parameter of populateView is already the value of each index of the list. So you just need to change the Map to 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;
    }
}    

Hope this helps :)

Upvotes: 1

Related Questions