Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Hey can anyone help me out clearing this error :

java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String at in.xyz.firebase.MainActivity$1.onDataChange

MainActivity.java

import java.lang.String;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

private Firebase mRef;

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

@Override
protected void onStart() {
    super.onStart();
    Button b1 = (Button) findViewById(R.id.buttonSunny);
    Button b2 = (Button) findViewById(R.id.buttonFoggy);
   final  TextView t1 = (TextView) findViewById(R.id.textView);

    mRef = new Firebase("https://docs-    examples.firebaseio.com/web/saving-data/fireblog/posts");

    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = (String) dataSnapshot.getValue();
            t1.setText(value);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}
}

Does anyone have any idea how to resolve this?

share|improve this question
1  
Well, it looks like getValue() is returning a HashMap. Take a look at the documentation: firebase.com/docs/java-api/javadoc/com/firebase/client/… – Daniel Nugent Nov 10 '15 at 3:36
up vote 1 down vote accepted

I guess you are using https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html API and your getValue() method is returning Hashmap.

You should do like this one -

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        HashMap<String, Object> yourData = dataSnapshot.getValue();
        // now get the data from the hashmap and set it to the text view

    }

Also as given in the documentation, getValue can return different type as well so you'll have to handle it accordingly.

share|improve this answer

Always use a POJO rather than a HashMap :)

Great answer by @Rahul Chaurasia! But I just wanted to expand upon how you return another class.

Let's say you have a class of Todo:

public class Todo {
  private String author;
  private String message;

  public Todo() {
    // empty default constructor, necessary for Firebase to be able to deserialize blog posts
  }

  public String getAuthor() {
    return author;
  }

  public String getMessage() {
    return message;
  }

}

Since this class has an empty constructor it's eligible for deserialization with Firebase.

  // Text view
  final TextView t1 = (TextView) findViewById(R.id.textView);

  // Get a reference to our posts
  Firebase ref = new Firebase("<my-firebase-db>/todos/1");

  // Get the todos
  ref.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
         Todo todo = snapshot.getValue(Todo.class);
         t1.setText(todo.getMessage());
      }

      @Override
      public void onCancelled(FirebaseError firebaseError) {
          System.out.println("The read failed: " + firebaseError.getMessage());
      }
  });

Using a class rather than a HashMap provides much better type-safety. And, it takes advantage of something that's just built-in to the Firebase SDK.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.