0

I'm trying to show data from my Firebase database to RecylerView and I'm following this tutorial http://www.programmingviral.com/firebase-recyclerview-tutorial/

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.biodatalist_layout);
        ButterKnife.bind(this);

        recyclerView = findViewById(R.id.rec);

        LinearLayoutManager horizontalLayoutManagaer=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(horizontalLayoutManagaer);

        mdatabase = FirebaseDatabase.getInstance().getReference().child("Biodata");
        mdatabase.keepSynced(true);

        FirebaseRecyclerAdapter<BiodataModel, BiodataAdapter> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BiodataModel, BiodataAdapter>(
                        BiodataModel.class,
                        R.layout.biodata_list_row,
                        BiodataAdapter.class,
                        mdatabase
                )
                {
                    @Override
                    protected void populateViewHolder(final BiodataAdapter viewHolder,final BiodataModel model,
                                                      int position) {
                        viewHolder.setEmailtext(model.getEmail());
                        viewHolder.setFullnametext(model.getFullname());

                    }

                };

        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

and my BiodataModel

public class BiodataModel {

    public String Fullname,Email,NoTelp,Alamat;

    public BiodataModel() {

    }

    public String getFullname() {
        return Fullname;
    }

    public String getEmail() {
        return Email;
    }

    public String getNoTelp() {
        return NoTelp;
    }

    public String getAlamat() {
        return Alamat;
    }

    public void setFullname(String Fullname) {
        this.Fullname = Fullname;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public void setNoTelp(String NoTelp) {
        this.NoTelp = NoTelp;
    }

    public void setAlamat(String Alamat) {
        this.Alamat = Alamat;
    }
}

when i run this activity . I get this error

com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: alamat

my structure

enter image description here

How can i solve this ?

CEO tech4lifeapps
  • 885
  • 1
  • 12
  • 31
YVS1102
  • 2,658
  • 5
  • 34
  • 63

2 Answers2

2

In your database, you have property: alamat with lowercase. But in the model class, you have the field Alamat with uppercase, both should be the same.

Change the field in the model class:

public String alamat;

public String getAlamat() {
    return alamat;
}

public void setAlamat(String alamat) {
    this.alamat = alamat;
}
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

You need to ensure that the property names of the JSON in the database, and those of your Java class match exactly. Right now your BiodataModel class has a (JavaBean) property alamat, while the database contains a (JSON) property Alamat. Since the case of the first character is different, Firebase can't find a matching property on your object when it reads the data from the database.

As Peter showed, the easiest fix is to keep the exact same case between database and code. An alternative is to annotate your Java code with the correct spelling/casing of the JSON property:

public class BiodataModel {

    public String Fullname,Email,NoTelp,Alamat;

    public BiodataModel() {

    }

    @PropertyName("Fullname")
    public String getFullname() {
        return Fullname;
    }

    @PropertyName("Email")
    public String getEmail() {
        return Email;
    }

    @PropertyName("NoTelp")
    public String getNoTelp() {
        return NoTelp;
    }

    @PropertyName("Alamat")
    public String getAlamat() {
        return Alamat;
    }

    @PropertyName("Fullname")
    public void setFullname(String Fullname) {
        this.Fullname = Fullname;
    }

    @PropertyName("Email")
    public void setEmail(String Email) {
        this.Email = Email;
    }

    @PropertyName("NoTelp")
    public void setNoTelp(String NoTelp) {
        this.NoTelp = NoTelp;
    }

    @PropertyName("Alamat")
    public void setAlamat(String Alamat) {
        this.Alamat = Alamat;
    }
}

Note by the way that the getters and setters are optional, and don't seem to add much here. Since your fields are already public, you can also use this much shorter class:

public class BiodataModel {
    public String Fullname;
    public String Email;
    public String NoTelp;
    public String Alamat;
}

Since the field names here already precisely match the JSON property names, you don't need to annotate them. But if you ever have a mismatch here, you can just annotate the field to get things working again. For example:

public class BiodataModel {
    @PropertyName("FullName")
    public String Fullname;
    public String Email;
    public String NoTelp;
    public String Alamat;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807