无法在RecyclerView中从Firebase获取数据 [英] Not getting data from Firebase in RecyclerView

查看:69
本文介绍了无法在RecyclerView中从Firebase获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都在尝试制作音乐应用,为此,我在HomeFragment中创建了一个水平RecyclerView,而我的水平RecyclerView正在获取带有艺术家姓名的图像.

everyone, I was trying to make a music app, and for this, I Created a Horizontal RecyclerView in my HomeFragment and my horizontal RecyclerView is getting an image with artist name.

但是在单击后,我加载了另一个活动.在我的另一项活动中,我试图使用RecyclerViewlistView的Firebase加载SongsData.

But after clicking I load another Activity. In my other activity, I was trying to load SongsData from firebase in a listView with RecyclerView.

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

But the problem is I am not getting data from Firebase and it is returning null data. I provided my code below and here is the screenshot of my Firebase database:- 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) {

                }
            });
        }
    }

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

Sorry for so much code but this is not hard to solve. If you find the solution please reply to me. Thanks for reading this.

推荐答案

代码中的问题在于,TestUploads类中的字段名称与数据库中的属性名称不同.您在TestUploads类中有一个名为songName的字段,但是在数据库中,我将其视为SongName,这是不正确的.名称必须匹配.当您使用名为getSongName()的吸气剂时,Firebase会在数据库中查找名为songName not SongName的字段.看到小写的s字母与大写字母S?

The problem in your code lies in the fact that the names of the fields in your TestUploads class are different than the name of the properties in your database. You have in your TestUploads class a field named songName but in your database, I see it as SongName and this is not correct. The names must match. When you are using a getter named getSongName(), Firebase is looking in the database for a field named songName and not SongName. See the lowercase s letter vs. capital letter S?

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

There are two ways in which you can solve this problem. The first one would be to remove the data in your database and add it again using field names that start with lowercase, as exist in your TestUploads class.

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

If you are not allowed to use the first solution, then the second approach will be to use annotations. So you should use the PropertyName annotation in front of the getters. So in your TestUploads class, a getter should look like this:

@PropertyName("SongName")
public String getSongName() {
    return songName;
}

这篇关于无法在RecyclerView中从Firebase获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆