Fouziya Parveen
Fouziya Parveen

Reputation: 31

Failed to read from Firebase

I am writing a program in which I need the value from firebase, the values are getting inserted perfectly, but I'm unable to read it. Below is my method to read the data. Please suggest where am I going wrong.

     private void displayChatMessage()
    {

        ListView listOfMessage = (ListView) findViewById(R.id.listview);

        adapter = new FirebaseListAdapter<ChatMessage>(MainActivity.this, ChatMessage.class, R.layout.list_item, FirebaseDatabase.getInstance().getReference()) {
            @Override
            protected void populateView(final View v, final ChatMessage model, final int position)
            {

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference("mychatapp-409f8").child("data");

                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                            ChatMessage model = dataSnapshot.getValue(ChatMessage.class);


                            TextView messageText, messageUser, messageTime;

                            messageText = (TextView) v.findViewById(R.id.msg_body);
                            messageUser = (TextView) v.findViewById(R.id.msg_user);
                            messageTime = (TextView) v.findViewById(R.id.msg_time);



                            messageText.setText(model.getmsgbody);
                            messageUser.setText(model.getUname());
                            messageTime.setText(DateFormat.format("dd-MM-yyyy(hh:mm a)", model.getMtime()));


                        }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        };

        listOfMessage.setAdapter(adapter);
    }


And my demo class is

    public class ChatMessage
{
    public String user;
    public long mtime;
    public String msgbody;
    public String uname;


    public ChatMessage(String user, String msgbody,String name)
    {
        this.user = user;
        this.msgbody = msgbody;
        this.uname = name;

        mtime = new Date().getTime();
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public ChatMessage() {
    }

    public String getUser() {
        return user;
    }

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

    public long getMtime() {
        return mtime;
    }

    public void setMtime(long mtime) {
        this.mtime = mtime;
    }

    public String getMsgbody() {
        return msgbody;
    }

    public void setMsgbody(String msgbody) {
        this.msgbody = msgbody;
    }

}

My firebase hierarchy is this:

enter image description here

Upvotes: 2

Views: 326

Answers (1)

koceeng
koceeng

Reputation: 2165

I still need the name of layout xml file that contains R.id.msg_body, R.id.msg_user, etc. But if we consider the layout name is R.layout.your_layout, then your code should look like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("data");
adapter = new FirebaseListAdapter<ChatMessage>(MainActivity.this, ChatMessage.class, R.layout.your_layout, ref) {
    @Override
    protected void populateView(final View v, final ChatMessage model, final int position) {
        TextView messageText, messageUser, messageTime;

        messageText = (TextView) v.findViewById(R.id.msg_body);
        messageUser = (TextView) v.findViewById(R.id.msg_user);
        messageTime = (TextView) v.findViewById(R.id.msg_time);

        messageText.setText(model.getmsgbody);
        messageUser.setText(model.getUname());
        messageTime.setText(DateFormat.format("dd-MM-yyyy(hh:mm a)", model.getMtime()));
    }
};

listOfMessage.setAdapter(adapter);

Hope this helps

Note: This is just a wild guess, I want to write it in comment because I'm not sure it will work, but I think comment field is not enough

Upvotes: 1

Related Questions