简体   繁体   中英

How to retrieve firebase data and create custom object

在此处输入图像描述

I have class object that is working when the comments section is removed from my database. When the comments are there I have this error "com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String".

My question is how to change my custom class to be able to handle this data. Can I create new comment class and the use the comment object inside my attraction object.

package com.example.androidproject;

import android.os.Parcel;
import android.os.Parcelable;

public class attraction implements Parcelable {
private String name;
private String latitude;
private String longitude;
private String rate;
private String totalVisiting;
private String imgSrc;
private String dateOfAdd;
private String createdBy;

public attraction() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public attraction(String name, String latitude, String longitude, int rate, int totalVisitors, 
String imgSrc,String createdBy) {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
    this.name = name;
}

protected attraction(Parcel in) { 
name = in.readString();
    latitude = in.readString();
    longitude = in.readString();
    rate = in.readString();
    totalVisiting = in.readString();
    imgSrc = in.readString();
    dateOfAdd = in.readString();
    createdBy = in.readString();

}

public static final Creator<attraction> CREATOR = new Creator<attraction>() {
    @Override
    public attraction createFromParcel(Parcel in) {
        return new attraction(in);
    }

    @Override
    public attraction[] newArray(int size) {
        return new attraction[size];
    }
};

public String getName(){
    return name;
}
public void setName(String name){
    this.name = name;
}
public String getLatitude(){
    return latitude;
}
public void setLatitude(String latitude){
    this.latitude = latitude;
}
public String getLongitude(){
    return longitude;
}
public void setLongitude(String longitude){
    this.longitude = longitude;
}
public String getRate(){
    return rate;
}
public void setRate(String rate){
    this.rate = rate;
}
public String getTotalVisiting(){
    return totalVisiting;
}
public void setTotalVisitors(String totalVisiting){
    this.totalVisiting = totalVisiting;
}
public String getImgSrc(){
    return imgSrc;
}
public void setImgSrc(String imgSrc){
    this.imgSrc = imgSrc;
}
public String getDateOfAdd(){
    return dateOfAdd;
}
public void setDateOfAdd(String dateOfAdd){
    this.dateOfAdd = dateOfAdd;
}
public String getCreatedBy(){
    return createdBy;
}
public void setCreatedBy(String createdBy){
    this.createdBy = createdBy;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(latitude);
    dest.writeString(longitude);
    dest.writeString(rate);
    dest.writeString(totalVisiting);
    dest.writeString(imgSrc);
    dest.writeString(dateOfAdd);
    dest.writeString(createdBy);
  

Here is where I create an object from my database

private void initializeListView() {
    atr = new ArrayAdapter<>(this, R.layout.custom, atrList);
    AttractionAdapter placeAdapter = new AttractionAdapter(this, atrList);
    setContentView(R.layout.activity_non_user);
    coursesLV = findViewById(R.id.idLVCourses);
    ListView listView = (ListView) findViewById(R.id.idLVCourses);

     firebaseAttraction = FirebaseDatabase.getInstance().getReference().child("attractions");


    firebaseAttraction.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot s : snapshot.getChildren()) {
                placeAdapter.add(a);
                setLocation(placeAdapter);

            }
        }

In the code everything is working without the comments section in the firebase but I want to retrieve all the data for one attraction and the create an object.

In order to read the values of comments you need to declare a new variable in attraction class named "comments" and its type will be new class with two String variables named "Image" and "text" make sure to declare variables as public or you can also provide getter and setter. so the final changes are:

public class ClassName{
    public String Image
    public String text
    ..constructor...
}

And in attraction class add new variable:

ClassName comments;

Modify the consturctor to and add this variable also provide getters and setters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM