简体   繁体   中英

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.

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

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

My List 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:-

    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. 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 ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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