Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Failed to bounce to type with FirebaseListadAdpter


I am working on Android Chat project and I have a ListView, which I want the messeges to populate through Firebase list adapter. Now after I run the code the app is crashing.

I try several ways to solve it with no success. Does my conception is wrong and i should use a different method? Anyway-

The exception is:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.example.giat.myapplication.ChatMessage] from String value; no single-String constructor/factory method

This is the main code in MainActivity.java:

    Firebase.setAndroidContext(this);
    mFirebaseRef = new Firebase("https://chat-as31da.firebaseio.com");

    Button sendBtn = (Button) findViewById(R.id.button);
    final EditText MessageTxt = (EditText) findViewById(R.id.edit_message);
 sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String text =MessageTxt.getText().toString();
            ChatMessage message = new ChatMessage("Android User", text);
            mFirebaseRef.push().setValue(message);
            MessageTxt.setText("");

        }
    });

    final ListView listView = (ListView) this.findViewById(android.R.id.list);
    mListAdapter = new FirebaseListAdapter<ChatMessage>(this,ChatMessage.class,android.R.layout.two_line_list_item,mFirebaseRef) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            ((TextView)v.findViewById(android.R.id.text1)).setText(model.get_name());
            ((TextView)v.findViewById(android.R.id.text2)).setText(model.get_messag());
        }
    };
    listView.setAdapter(mListAdapter);


}

And this is the POJO:

public class ChatMessage {

private String _name;
private String _message;

public ChatMessage() {}

public ChatMessage(String _name,String _message ) {
    this._name = _name;
    this._message = _message;
 }

public String get_name() {
    return _name;
 }

public String get_message() {
    return _message;
  }
}

JASON Snippet of data:

{
"_message" : "hi there",
"_name" : "sam"
}

Thanks for helping!


Solution

  • First of all, I found that I need to connect to Firebase database like this:

            mFirebaseRef = new Firebase("https://<stack-overflow>.firebaseio.com/mychat");
    

    Second, to solve the Jackson problem (names starting with underscore), with the help of @Frank van Puffelen I found that I need to make ChatMessage class as static in MainActivity and delete the setters/getters.

    Or

    I can just add @JsonProperty before my variables declaration and the getters in ChatMessage class.