0

I'm trying to get data from Firestore and add them to a recycler view, I tried doing this:

for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
    if (doc.getType() == DocumentChange.Type.ADDED) {

        Users users = doc.getDocument().toObject(Users.class);

        if (users.getName() != null) {                
            if(users.getStatus() == null){                    
                mUsersList.add(new Users(true));
                mUsersList.add(users);                    
            }

            mUsersList.add(users);
            usersListAdapter.notifyDataSetChanged();
        }
    }
}

I have an adapter already configured in the proper way, but when I try setting the Boolean flag, that is required for showing one layout or the other this appears:

java.lang.RuntimeException: Could not deserialize object. Class com.example.gusta.client.list_model.Users does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

This I because I need an empty constructor for this, but if I set an empty constructor on my List, I wont be able to choose between the layouts.

If you know the answer or knows a video class that could help me solving this problem I would be very glad, thanks.

public class Users {

    String user,status,image,descricao;
    private Boolean adcionais;



        public Users(){

        }

        public Users(String name, String status,Boolean adcionais) {
            this.user = name;
            this.status = status;
            this.adcionais = adcionais;
        }

        public String getName() {
            return ;
        }

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

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getDescricao() {
            return descricao;
        }

        public void setDescricao(String descricao) {
            this.descricao = descricao;
        }


        public Boolean getAdcionais() {
            return adcionais;
        }

        public void setAdcionais(Boolean adcionais) {
            this.adcionais = adcionais;
        }
    }
  • 1
    I gave the most likely cause and solution below. If that isn't the problem, edit your question to include your `Users` class. In general, I recommend reading [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) for great advice on how to make it most likely we can quickly help you with code-related questions. – Frank van Puffelen Jan 16 '19 at 05:41
  • 1
    If you are trying to create a RecyclerView with multiple Layouts(ViewHolder) then this might help you https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type/51153083#51153083 – Rohit Singh Jan 16 '19 at 05:43

3 Answers3

1

As the error message says, your Users class doesn't have a no-argument constructor, which is required for the Cloud Firestore SDK to be able to create the class from the JSON data in the document.

To solve this, add a no-argument constructor to your class:

public class Users ... {
    ...
    public Users() { }
    ...
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hey Frank, I edited my question and add the User class, I have the empty constructor, but I need to populate him with a Boolean to set different layouts in the recyclerview. – gustavo martins Jan 16 '19 at 05:52
  • You must have an empty constructor, because that's the one Firebase calls when it needs to create a new instance of your class for a document when you call `toObject()`. You can set the boolean default in the no-argument constructor. – Frank van Puffelen Jan 16 '19 at 06:02
1

You need to initialize the variable with default values. Either you can initialize the variables where you have defined them or you can use as shown below:

public class Users ... {
...
public Users() { 
    Users("","",false) // "" or null for String
  }
  ...
}

Also, add implement Serializable to your model.

nitinkumarp
  • 2,120
  • 1
  • 21
  • 30
0

Instead of the Boolean, I used this:

        @Override
          public int getItemViewType(int position) {

        if(mUsersList.get(position).getStatus() ==null){

                return CLASSE;

        }else {

               return USER;
        }

    }

}

So now its working.