5

I have been getting this error "Expected a Map while deserializing, but got a class java.lang.Long" while i use firebase database. I looked up some question the same problem on stackoverflow but didn't found a relevant solution.

This is my POJO class for storing the data from firebase.

public class News {
public String topic;
public String detail;
public String user;
public Map<String, String> timestamp = null;

public News(){

}

public News(String topic, String detail, String user, HashMap<String, String> timeStamp){
    this.topic = topic;
    this.detail = detail;
    this.user = user;
    this.timestamp = timeStamp;
    //this.timestamp.put("timestamp", timeStamp);
    //this.timestamp.put("timestamp", ServerValue.TIMESTAMP);
}

public String getTopic(){
    return topic;
}

public String getDetail(){
    return detail;
}

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();

    result.put("topic", topic);
    result.put("detail", detail);
    result.put("user", user);
    result.put("timestamp", timestamp);

    return  result;
}
}

And this the main class where I get data from the the firebase.

public class News_M extends AppCompatActivity {

FirebaseListAdapter mAdapter;

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

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

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("abc").child("news");
    //News msg = new News("Holiday", "28 October", "fayeed", ServerValue.TIMESTAMP);
    //ref.push().setValue(msg);

    mAdapter = new FirebaseListAdapter<News>(this, News.class, android.R.layout.two_line_list_item, ref) {
        @Override
        protected void populateView(View view, News newsMessage, int position) {
            ((TextView)view.findViewById(android.R.id.text1)).setText(newsMessage.getTopic());
                 ((TextView)view.findViewById(android.R.id.text2)).setText(newsMessage.getDetail(      ));
        }
    };

    messagesView.setAdapter(mAdapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mAdapter.cleanup();
}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
fayeed
  • 2,375
  • 16
  • 22

0 Answers0