简体   繁体   English

未从 RecyclerView 中的 Firebase 获取数据

[英]Not getting data from Firebase in 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.大家,我正在尝试制作一个音乐应用程序,为此,我在HomeFragment中创建了一个水平RecyclerView ,我的水平RecyclerView正在获取带有艺术家姓名的图像。

But after clicking I load another Activity.但点击后我加载了另一个活动。 In my other activity, I was trying to load SongsData from firebase in a listView with RecyclerView .在我的其他活动中,我试图在带有RecyclerViewlistView中从 firebase 加载 SongsData。

But the problem is I am not getting data from Firebase and it is returning null data.但问题是我没有从 Firebase 获取数据,它正在返回 null 数据。 I provided my code below and here is the screenshot of my Firebase database:- ScreenShot我在下面提供了我的代码,这是我的 Firebase 数据库的屏幕截图:- ScreenShot

My List Class:-我的清单 Class:-

    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;

        }
    }

My Adapter Class:-我的适配器 Class:-

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

            }
        }

    }

My Activity:-我的活动:-

    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.感谢您阅读本文。

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.代码中的问题在于TestUploads class 中的字段名称与数据库中属性的名称不同。 You have in your TestUploads class a field named songName but in your database, I see it as SongName and this is not correct.您在TestUploads class 中有一个名为songName的字段,但在您的数据库中,我将其视为SongName ,这是不正确的。 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 .当您使用名为getSongName()的 getter 时,Firebase 正在数据库中查找名为songName不是SongName的字段。 See the lowercase s letter vs. capital letter S ?看到小写字母s与大写字母S了吗?

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.第一个是删除数据库中的数据并使用以小写开头的字段名称再次添加它,如TestUploads class 中存在的那样。

If you are not allowed to use the first solution, then the second approach will be to use annotations .如果您不允许使用第一种解决方案,那么第二种方法将是使用annotations So you should use the PropertyName annotation in front of the getters.所以你应该在 getter 前面使用PropertyName注释。 So in your TestUploads class, a getter should look like this:因此,在您的TestUploads class 中,getter 应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM