首页 > 解决方案 > 无法将字符串转换为 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)

我的杰森:

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

我的 DjProfile 类定义如下:

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 recyclerview 的 ViewHolder 类:

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");

标签: androidfirebasefirebase-realtime-databasefirebaseui

解决方案


更改您的数据库如下:

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

然后您就可以将您的 POJO 与数据库进行映射。


推荐阅读