1

I used FirebaseRecyclerAdapter to get all the childs of "Pro" using a Model class named " Spacecraft" and now I want to retrieve all the candidates into a child of Pro like "1"

I created a public static "candidat" into "Spacecraft" and I used the setters and getters but still the same error

This is my database:

Database

this is the Model Class

public class Spacecraft{
    private String name;
    private String desc;
    private String last;
    private candidat candidat;

    public Spacecraft.candidat getCandidat() {
        return candidat;
    }

    public void setCandidat(Spacecraft.candidat candidat) {
        this.candidat = candidat;
    }

    public Spacecraft() {
    }
    public String getName() {

        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }
    public static class candidat{

        private String info;
        private String namecandid;
        public candidat(){}
        public String getInfo() {
            return info;
        }

        public void setInfo(String info) {
            this.info = info;
        }

        public String getNamecandid() {
            return namecandid;
        }

        public void setNamecandid(String namecandid) {
            this.namecandid = namecandid;
        }
    }
}

This is my code for FirebaseRecyclerAdapter

@Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Spacecraft, candidatviewholder> firebaseRecyclerAdapter  = new FirebaseRecyclerAdapter<Spacecraft, candidatviewholder>(
                Spacecraft.class,
                R.layout.candidat,
                candidatviewholder.class,
                query){
                        @Override
                        protected void populateViewHolder(candidatviewholder viewHolder, Spacecraft model, int position) {
                            viewHolder.setName1(model.getCandidat().getNamecandid());
                            viewHolder.setInfo1(model.getCandidat().getInfo());
                        }
                    };
        rv.setAdapter(firebaseRecyclerAdapter);
    }

The error:

No setter/field for key1 found on class com.example.ilyas.evotingapplication.Spacecraft$candidat

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ilyas shabi
  • 194
  • 1
  • 7

3 Answers3

4

I had this error but the above solutions didn't fix it. Hopefully, this alternate solution will help others. If you have that error occur for almost every variable, chances are that you have Proguard enabled and it is removing the un-used getter and setter methods. To fix this, add a line similar to this to your proguard-rules.pro file:

-keep class com.example.yourapp.ObjectClass

where ObjectClass is the name of your java object class that is stored to Firebase.

Peter Keefe
  • 1,095
  • 14
  • 22
1

I think it's just that your data models on Firebase and in Java differ.

In your java class, the Spacecraft class has a candidat field of type Candidat. But, in the database, the candidat field is really a nested object (map), containing one key Key1, which value is a Candidat structure.

So, depending on what did you want to achieve:

  • if you wanted each spacecraft to have exactly one candidat: save the database object properly, so {info: "info 1", namecandid: "name 1"} is saved directly under candidat field, not one level deeper, so the field has type Candidat in the code.
  • if you wanted each spacecraft to have a few candidats: instead of private Candidat candidat field, it should be typed Map<String, Candidat>, because that's the type it has in your database screenshot.
quezak
  • 1,233
  • 1
  • 14
  • 30
  • Hey quezak, thank u for ansewring ! it's the second case; i want each spacecrft to have few candidats and populate them into a FirebaseRecycler; i added this to Spacecraft " private Map candidat; " (with getters, setters) Can u tell me how can i retrieve data of this Map ? – ilyas shabi Jan 14 '17 at 15:07
  • @ilyasshabi iterate over it like a normal Java map :) see [this answer](http://stackoverflow.com/a/1066607/1815209) for a simple explanation. – quezak Jan 14 '17 at 15:17
  • i'll iterate it into this function populateViewHolder ? – ilyas shabi Jan 14 '17 at 15:20
  • @ilyasshabi I didn't use recycler adapters myself, so just the general idea -- if your query returns the whole spacecraft then inside iterate over the candidats map, or make a query that just returns all the candidats of one ship, then you don't have to deal with iteration inside. – quezak Jan 14 '17 at 15:36
0

Work for me: -keepclassmembers class com.myPackageName.MyClassName { *; }

Leonid Ivankin
  • 579
  • 4
  • 9