FirebaseRecyclerAdapter但无法将java.util.HashMap类型的值转换为String?

时间:2017-09-30 00:56:36

标签: java android firebase-realtime-database firebaseui

尝试创建FirebaseRecyclerAdapter,从对象类中检索所有数据,但是我收到错误“无法将java.util.HashMap类型的值转换为String” 这是我的代码:

public class Match {


    private Map<String, Object> name;
    private Map<String, Object> desc;

    public Map<String, Object> getName() {
        return name;
    }

    public Map<String, Object> getDesc() {
        return desc;
    }

    public Match(){} //even with /without this guy
}


public class MatchHolder extends RecyclerView.ViewHolder {
    private final TextView mNameField;
    private final TextView mMessageField;

    public MatchHolder(View itemView) {
        super(itemView);
        mNameField = (TextView) itemView.findViewById(android.R.id.text1);
        mMessageField = (TextView) itemView.findViewById(android.R.id.text2);
    }

    public void setName(Map<String, Object> name) {
        mNameField.setText((CharSequence) name);
    }

    public void setDesc(Map<String, Object> desc) {
        mMessageField.setText((CharSequence) desc);
    }
}



public class SportActivity extends AppCompatActivity {

    final FirebaseDatabase database = FirebaseDatabase.getInstance();



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

 DatabaseReference ref = database.getReference("myApp/sportActivity"); //with this guy i get the mentioned error

        //if use         DatabaseReference ref = database.getReference("myApp/sportActivity/Match");
        //i get in the log cat ,error W/ClassMapper: No setter/field for name1,name2 ,desc1,desc2 ...etc    
         RecyclerView matches = (RecyclerView) findViewById(recycle_viewer);
        matches.setHasFixedSize(true);
       matches.setLayoutManager(new LinearLayoutManager(this));


        FirebaseRecyclerAdapter<Match, MatchHolder> mAdapter = new FirebaseRecyclerAdapter<Match, MatchHolder>(
                Match.class,
                android.R.layout.two_line_list_item,
                MatchHolder.class,
                ref) {
            @Override
            public void populateViewHolder(MatchHolder holder, Match match, int position) {
                holder.setName(match.getName());
                holder.setDesc(match.getDesc());
            }
        };

        matches.setAdapter(mAdapter);



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FirebaseRecyclerAdapter mAdapter = null;
        mAdapter.cleanup();
    }


    }

我无法上传我的数据库的图片,但结构是: 对myApp / sportActivity / 这里是sportActivity的Json:

"sportActivity" : {
      "Match" : {
        "desc" : {
          "des3" : "desc3",
          "desc2" : "desc2"
        },
        "name" : {
          "name1" : "name1",
          "name2" : "name2"
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

Match对象具有以下结构:

{
  "desc" : {
    "des3" : "desc3",
    "desc2" : "desc2"
  },
  "name" : {
    "name1" : "name1",
    "name2" : "name2"
  }
}

您用来读取此数据的Java类是:

public class Match {
   private String name; 
   private String desc;

   public Match() { }

   public String getName() { return name; }
   String getDesc() { return desc; }
}

这些不匹配:您的Java类说desc是一个字符串,但它实际上是一个嵌套的JSON对象。由于该嵌套JSON对象没有类,因此Firebase会将其读作Map<String, Object。由于您的desc属性是String,因此它是一种不兼容的类型,会导致类强制转换异常。

要了解有关如何建模嵌套对象的更多信息,请在此处查看我的答案:Creating Nested Object Firebase structure

更新:应用于您的JSON,最小的Java类似乎是:

public class Match {
    private Map<String, Object> name;
    private Map<String, Object> desc;

    public Map<String, Object> getName() {
        return name;
    }

    public Map<String, Object> getDesc() {
        return desc;
    }
}