0

I am a newbie in Android application development. I am trying to retrieve data from Firebase but i am not able to type cast string to user defined object.Firstly it was giving me ClasscastException that it cannot convert string to user-defined object.

here is my code:

MainActivity.java

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {

    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference databasereference;
    ListView l1;
    List<User> names;
    ArrayAdapter<User>arrayAdapter;
    Button b;
    User u = new User();

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

        l1 = findViewById(R.id.listvw);
        b = findViewById(R.id.list_button);

        b.setOnClickListener(this);
 }

public void onClick(View view)
{
     retrieve_data();
}

public void retrieve_data()
    {
        databasereference = db.getReference("User");
        names = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);
        databasereference.addValueEventListener(new ValueEventListener() {

           // User u1,u2;

           // String s1,s2;
            public void onDataChange(DataSnapshot datasnapshot) {
                try {
                    names.clear();
                    for (DataSnapshot ds : datasnapshot.getChildren()) {
                      User u1=(User) ds.child("name").getValue(User.class);
                    User u2 =(User) ds.child("contact").getValue(User.class);
                   names.add(u1);
                   names.add(u2);                        
                   }
                   l1.setAdapter(arrayAdapter);

                } catch (Exception e) {
                    // Toast.makeText(this,"failed",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
        }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}

User.java

public class User
{
    String name;
    String contact;
    String id;
    String tmp;

    public User() {
    }


    public User(String name,String contact,String id) {
        this.name = name;
        this.contact = contact;
        this.id = id;
    }

    public User(String tmp)
    {
        this.tmp=tmp;
    }
    public User(String name,String id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;

    }

}

database structure in firebase

Exception details:

 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.firebase_smpl.User
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
    at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
    at com.example.firebase_smpl.MainActivity3$1.onDataChange(MainActivity3.java:118)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5628)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)

someone,please help me. I am stuck here.

Note: I changed this line : User u1=(User) ds.child("name").getValue(User.class); To this line: User u1=ds.getValue(User.class); according to suggestion but it is giving me above error:

Firebase list output

2 Answers2

0

It should be like...

public class User implements Serializable

User user = ds.getValue(User.class); // ds will be your whatever datasnapshot node

0

You are getting the following error:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.firebase_smpl.User

Most likely at this line of code:

User u1=(User) ds.child("name").getValue(User.class);

And this is because ds.child("name") returns a String and not a User object. If you need to get each User object under your User node, then please change the above line of code to:

User u1 = ds.getValue(User.class);

Where a casting is not needed at all. If you only need to get the name, then please change it to:

String name = ds.child("name").getValue(String.class);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193