com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.bottary.Movie类型

时间:2019-06-09 15:14:48

标签: java android

我试图在recyclerview中显示图像和textview,但是我找不到问题,我将照片和文本放在firebase中的“”是什么问题?

  

Java

Input: 50
50, [255,128,0]

Input: 33
33, [168,171,0]

有模型类

  

Java

    MoviesRv.setLayoutManager(new LinearLayoutManager(HomeActivity.this, LinearLayout.HORIZONTAL,false));
    reference = FirebaseDatabase.getInstance().getReference("Data");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            List<Movie> first_tMovie = new ArrayList<>();
            for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
            {
                Movie p = dataSnapshot1.getValue(Movie.class);
                first_tMovie.add(p);
            }
            MovieAdapter movieAdapter = new MovieAdapter(HomeActivity.this,first_tMovie);
            MoviesRv.setAdapter(movieAdapter)

1 个答案:

答案 0 :(得分:0)

这是因为您不能将Movie.class的参数传递给value参数:

改为更改此内容:

  Movie p = dataSnapshot1.getValue(Movie.class);

对此:

Movie p = (Movie)dataSnapshot1.getValue();

或正确传递参数,即

Movie p = new Movie(datasnapshot1.child("x").getValue(),datasnapshot1.child("thumbnail").getValue());