0

My Android FirebaseUI Recycler view is responding well with String class datatype. But is not populating view when I use my POJO class as datatype.

I have taken time to ensure the names in my POJO matches the names in JSON file. Below are my codes:

public class MainActivity extends AppCompatActivity   {
    public static class MessageViewHolder extends RecyclerView.ViewHolder {
        TextView messageTextView;

        public   MessageViewHolder(View view) {
            super(view);
            messageTextView =(TextView) messageTextView.findViewById(android.R.id.text1);

        }

    }

    android.support.v7.widget.RecyclerView recyclerView;

    FirebaseDatabase  database = FirebaseDatabase.getInstance();

    DatabaseReference myRef = database.getReference();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        //Recycler
        recyclerView=(android.support.v7.widget.RecyclerView)findViewById(R.id.recyclerview);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //Adapter
             FirebaseRecyclerAdapter<User, MessageViewHolder> mFirebaseAdapter=
                new FirebaseRecyclerAdapter<User, MessageViewHolder>(User.class,
                        R.layout.layout,
                        MessageViewHolder.class,
                        myRef.child("User")
                ) {
                    @Override
                    protected void populateViewHolder(MessageViewHolder viewHolder, User model, int position) {

                        viewHolder.messageTextView.setText(model.getName());
                    }
                };


        recyclerView.setAdapter(mFirebaseAdapter);


    }
}

POJO:

public class User {

   public User(){

    }
    String Name;
    String email;
   public User(String Name){

        this.Name=Name;

    }

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

    public String getName() {
        return Name;
    }

}

Error From Firebase

Exception com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.projectgenuis.eneanews.User

EDIT(JSON)

 {
  "User" : {
    "Name" : "Zion",

  }
Ebite Zion
  • 340
  • 2
  • 10
  • Please share the JSON (as text, no screenshots) of the `/User` node in yoru database. You can easily get this by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). – Frank van Puffelen Jan 12 '17 at 14:37
  • JSON:{ "User" : { "Name" : "Zion", } – Ebite Zion Jan 12 '17 at 14:49

1 Answers1

2

The FirebaseUI adapters are designed to show lists of items, in your case a list of users. In your current data model, there is only a single user under the /User node. That means there is no way to show it in a list view.

The simplest way to fix this is to change your JSON to have a list of users under a Users node:

{
  "Users" : {
    "User1": {
      "Name" : "Zion"
    }
    "User2": {
      "Name" : "puf"
    }
  }
}

And then attach the adapter to this collection:

mFirebaseAdapter= new FirebaseRecyclerAdapter<User, MessageViewHolder>(
                    User.class,
                    R.layout.layout,
                    MessageViewHolder.class,
                    myRef.child("Users")
            ) {
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for answering,i followed your suggestions and had this time the app is crashing with three new errors: `Exception java.lang.RuntimeException: java.lang.reflect.InvocationTargetException Caused by java.lang.reflect.InvocationTargetException: Caused by java.lang.NullPointerException:` – Ebite Zion Jan 12 '17 at 15:54
  • Since these are new, it sounds like you're making progress and it may be a new problem. Before posting, be sure to read how to troubleshoot a `NullPointerException`: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Jan 12 '17 at 16:34
  • Just to be sure the null pointer error is not from my POJO,i changed the class datatype in the adapter to string and it still crashes with the same Error. I followed the tutorials but can't seem to figure it out.My POJO seems in order. `Exception java.lang.RuntimeException: java.lang.reflect.InvocationTargetException Caused by java.lang.reflect.InvocationTargetException: Caused by java.lang.NullPointerException: com.projectgenuis.eneanews.MainActivity$MessageViewHolder.` – Ebite Zion Jan 13 '17 at 12:59
  • i did a little adjustment to the aforementioned code and realized that using the `String` data type it works well with an adjustment in the JSON,like so:`{User{"name":Zion,"gender":"male"},}` but the POJO is still unresponsive.Further both the POJO and the String class will not respond to the JSON structure you suggested.Your insights will be highly valued. – Ebite Zion Jan 14 '17 at 09:36
  • Thanks you very much.The problem actually is from my JSON.I learnt a lot from your valuable insight. I also had to re-order my POJO to reflect the changes, Frank thanks again it meant a lot to me. – Ebite Zion Jan 15 '17 at 16:08