小编典典

无法在RecyclerView中从Firebase获取数据

java

每个人,我都试图制作一个音乐应用程序,为此,我RecyclerView在自己的作品中创建了一个Horizo​​ntal
HomeFragment而我的Horizo​​ntal RecyclerView正在获取带有艺术家姓名的图像。

但是单击后,我加载了另一个活动。在我的其他活动,我试图在从火力加载SongsData listViewRecyclerView

但是问题是我没有从Firebase获取数据,而是返回了空数据。我在下面提供了我的代码,这是我的Firebase数据库的屏幕截图:-ScreenShot

我的清单类别:-

    public class TestUploads
    {
        private String songName;
        private String songImageUri;
        private String songUrl;
        private String artistName;

        public TestUploads() {
        }

        public String getSongName() {
            return songName;
        }

        public void setSongName(String SongName) {
            this.songName = SongName;
        }

        public String getSongImageUri() {
            return songImageUri;
        }

        public void setSongImageUri(String SongImageUri) {
            this.songImageUri = SongImageUri;
        }

        public String getSongUrl() {
            return songUrl;
        }

        public void setSongUrl(String SongUrl) {
            this.songUrl = songUrl;
        }

        public TestUploads(String SongImageUri, String SongName, String SongUrl ) {
            this.songName = SongName;
            this.artistName = SongImageUri;
            this.songUrl = SongUrl;

        }
    }

我的适配器类别:-

    public class TestAdapter extends RecyclerView.Adapter<TestAdapter.TestViewHolder>{

        private Context mContext;
        private List<TestUploads> mUploads;

        public TestAdapter(Context context , List<TestUploads> uploads) {
            mContext = context;
            mUploads = uploads;
        }

        @NonNull
        @Override
        public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.test_package_layout , parent ,false);
            return new TestViewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull TestViewHolder holder, int position) {


            TestUploads uploadcurrent = mUploads.get(position);

            holder.name.setText(uploadcurrent.getSongName());

            Glide.with(mContext)
                    .load(uploadcurrent.getSongImageUri())
                    .into(holder.image_view);

        }

        @Override
        public int getItemCount() {
            return mUploads
                    .size();
        }


        public class TestViewHolder extends RecyclerView.ViewHolder {

            public TextView name;
            public TextView artist_name;
            public CircleImageView image_view;

            public TestViewHolder(@NonNull View itemView) {
                super(itemView);

                name = itemView.findViewById(R.id.test_package_song_name);
                artist_name = itemView.findViewById(R.id.test_package_artist_name);
                image_view = itemView.findViewById(R.id.test_package_image_name);

            }
        }

    }

我的活动:

    public class TestActivity extends AppCompatActivity {

        private ValueEventListener listener;
        private DatabaseReference reference;
        private List<TestUploads> mUploads;

        private RecyclerView mRecyclerView;

        private TestAdapter adapter;


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

            reference = FirebaseDatabase.getInstance().getReference("ArtistView").child(getIntent().getStringExtra("Artist"))
            .child("Songs");

            Toast.makeText(this, "" + getIntent().getStringExtra("Artist"), Toast.LENGTH_SHORT).show();

            mUploads = new ArrayList<>();

            mRecyclerView = findViewById(R.id.test_pacakge_recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.smoothScrollToPosition(0);


            adapter = new TestAdapter(this , mUploads);

            mRecyclerView.setAdapter(adapter);

            listener = reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    mUploads.clear();

                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        TestUploads uploads =postSnapshot.getValue(TestUploads.class);
                        mUploads.add(uploads);
                    }

                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }

很抱歉有这么多代码,但这并不难解决。如果您找到解决方案,请回复我。感谢您阅读本文。


阅读 210

收藏
2020-11-30

共1个答案

小编典典

您的代码中的问题在于,您的TestUploads类中的字段名称与数据库中的属性名称不同。您的TestUploads班级中有一个名为的字段,songName但是在您的数据库中,我认为是SongName,这是不正确的。名称必须匹配。当您使用一个名为吸气getSongName(),火力地堡是寻找数据库名为场songName
没有 SongName。看到小写s字母还是大写字母S

有两种方法可以解决此问题。第一个是删除数据库中的数据,并使用小写开头的字段名称(如您的TestUploads类中存在的那样)再次添加数据。

如果不允许使用第一种解决方案,则第二种方法是使用annotations。因此,您应该在吸气剂之前使用PropertyName批注。因此,在您的TestUploads课堂上,吸气剂应如下所示:

@PropertyName("SongName")
public String getSongName() {
    return songName;
}
2020-11-30