PowerMan2015
PowerMan2015

Reputation: 1418

FirebaseListAdapter Access to key

Im using Google Firebase and FirebaseUI

I am passing a list of values to a FirebaseListAdapter like so

 ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
                requestList request = requestSnapshot.getValue(requestList.class);
                Log.i("Chat", request.getTitle()+": "+request.getDescription()+"    "+request.getKey());
            }

I want to store the index of the record (Key) along with the information within. However as the fields automatically map themselves if they are the same name, this leaves my unable to work it out.

I have tried calling the properties of the "key" methods within the requestList class by the name "key" however this hasnt worked

The returned data structure looks like this

    Key:{
    "RequestTitle":"Value",
    "RequestDescription":"description",
    "RequestItems":{ item 1 item 2}
}

The requests class looks like this

public class requestList {

    String RequestTitle;
    String RequestDescription;
    String key;

    public requestList() {
    }

    public requestList(String RequestTitle, String RequestDescription, String key) {
        this.RequestTitle = RequestTitle;
        this.RequestDescription = RequestDescription;
        this.key = key;
    }

    public String getTitle() {
        return RequestTitle;
    }

    public String getKey() {
        return key;
    }

    public String getDescription() {
        return RequestDescription;
    }
}

Upvotes: 0

Views: 207

Answers (2)

PowerMan2015
PowerMan2015

Reputation: 1418

Ive upvoted the answer above because i couldnt have dont it with the input, however here is the working answer

i changed my requestList class to include a setKey method and then called this from within the onDataChange method. Not sure if its the correct method, but it certainly solves the problem

    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot requestSnapshot: snapshot.getChildren()) {
            //requestList request = requestSnapshot.getValue(requestList.class);
            requestList request = requestSnapshot.getValue(requestList.class);
            request.setKey(requestSnapshot.getKey());
            Log.i("Chat", request.getTitle()+": "+request.getDescription()+"    "+request.getKey());
        }
    }

and the requestList Class

public class requestList {

    String RequestTitle;
    String RequestDescription;
    String key;

    DataSnapshot requestSnapshot;


    public requestList() {
    }

    public requestList(DataSnapshot requestSnapshot) {
        //this.RequestTitle = RequestTitle;
        //this.RequestDescription = RequestDescription;
        //this.key = key;

        this.requestSnapshot = requestSnapshot;
    }

    public String getTitle() {
        return RequestTitle;
    }

    public String getKey() {
        return key;
    }

    public String getDescription() {
        return RequestDescription;
    }


    public void setKey(String key) {
        this.key = key;
    }
}

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

When you call requestSnapshot.getValue(requestList.class) you are taking the JSON value from the database and turning it into a requestList object. The key is not part of that.

However, you can easily call requestSnapshot.getKey() to get the key of each snapshot.

If you want to keep both the key and the value, it might be worth simply keeping the DataSnapshot since that contains both of them.

Upvotes: 1

Related Questions