无法将字符串转换为 DjProfile(firebase)

Cannot convert String to DjProfile(firebase)

我正在尝试从我的 json 获取数据以通过 firebase recyclerview 显示,但我 运行 遇到此错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android.heydj.DjProfile
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.6:423)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.6:214)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.6:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.6:212)
    at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
    at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
    at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
    at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)

我的Json:

 "allDeeJays" : {
    "-Le5FguoZsnT2lejgAkZ" : "yousucks",
    "-Le5IsCPK69NTbgbgUUo" : "ffffggg",
    "-Le5J5CmPkddSaK_MgpG" : "ahhhaahhaahha"
  }

我的 DjProfile class 定义如下:

public class DjProfile
{
    String djName;
    String googleUserName;

    public DjProfile(String djName, String googleUserName)
    {
        this.djName = djName;
        this.googleUserName = googleUserName;
    }
    public DjProfile(){}

    public void setdjName(String djName)
    {
        this.djName = djName;
    }

    public String getdjName()
    {
        return djName;
    }
}

我的 firebase recyclerview 如下:

query = mProfileDatabase.child("allDeeJays")
        .orderByValue()
        .startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");

FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
        .setQuery(query, DjProfile.class)
        .build();


firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{

    @NonNull
    @Override
    public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
        return new ResultsViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ResultsViewHolder holder, final int position, @NonNull final DjProfile model) {
        holder.setDjProfile(model);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplication(), AddSongRequest.class);
                i.putExtra("DjName", model.getdjName());
                startActivity(i);
            }
        });
    }
};
firebaseRecyclerAdapter.startListening();

Firebase 回收视图的 ViewHolder class:

public static class ResultsViewHolder extends RecyclerView.ViewHolder
{
    private TextView djNameView;

    public ResultsViewHolder(View itemView)
    {
        super(itemView);
        djNameView = itemView.findViewById(R.id.result_dj);
    }

    void setDjProfile(DjProfile profile)
    {
        String djName = profile.getdjName();
        profile.setdjName(djName);
    }
}

感谢任何帮助:)

编辑 我的 JSON 现在格式为:

  "allDeeJays" : {
    "-LeNGZKDoIcVOLUokvrP" : {
      "djName" : "ahhahaha"
    },
    "-LeNGb2sy9qG_U0zA1xS" : {
      "djName" : "newdj"
    }
  }

现在我无法使用以下方法将 name 的值显示到 recyclerview 中:

            query = mProfileDatabase.child("allDeeJays")
                    .orderByValue()
                    .startAt(t.getText().toString()).endAt(t.getText().toString() + "\uf8ff");

更改数据库如下:

allDeeJays
     PushId
       djName         : "john"
       googleUserName : "john"

然后您就可以将您的 POJO 映射到数据库。

您的 DjProfile class 定义了一个 JSON 结构,如下所示:

{
    djName: "Name of DJ"
}

所以这些 DJ 的列表如下所示:

"allDeeJays" : {
  "-Le5FguoZsnT2lejgAkZ" : { "djName": "..." },
  "-Le5IsCPK69NTbgbgUUo" : { "djName": "ffffggg" },
  "-Le5J5CmPkddSaK_MgpG" : { "djName": "ahhhaahhaahha" }
}

您将查询上述结构:

query = mProfileDatabase.child("allDeeJays")
                .orderByChild("djName")
                .startAt("f").endAt("f\uf8ff")

由于您的 JSON 看起来不同,Firebase 将无法转换它,这就是您获得异常的原因。

您可以更改您的 JSON 以匹配您的代码,这是 Peter 回答的,或者您可以更改您的代码以匹配 JSON。

使用 FirebaseUI 执行后者意味着您将不再告诉 FirebaseUI 使用您的自定义 class,但它必须使用原始快照。代码看起来像这样:

FirebaseRecyclerOptions<DataSnapshot> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DataSnapshot>()
        .setQuery(query, new SnapshotParser<DataSnapshot>() {
            @Override
            public DataSnapshot parseSnapshot(DataSnapshot snapshot) {
                return snapshot;
            }
        }).build();

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DataSnapshot, ResultsViewHolder>(firebaseRecyclerOptions)
{

    @NonNull
    @Override
    public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
        return new ResultsViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ResultsViewHolder holder, final int position, @NonNull final DataSnapshot snapshot) {
        // Here you convert the DataSnapshot to whatever data your ViewHolder needs
        DjProfile model = new DjProfile(snapshot.getKey(), snapshot.getValue(String.class));

        holder.setDjProfile(model);
        ...
    }
};

此处新的快照解析器基于this comment:

另见 documentation of FirebaseUI on SnapshotParser