package com.example.stdio9.fragment;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.stdio9.Adapter.ContentAdapter;
import com.example.stdio9.Models.ContentModel;
import com.example.stdio9.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Collections;
public class HomeFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<ContentModel> list;
ContentAdapter adapter;
DatabaseReference reference;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
reference = FirebaseDatabase.getInstance().getReference().child("Content");
getAllVideos();
return view;
}
private void getAllVideos() {
list = new ArrayList<>();
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
list.clear();
for(DataSnapshot dataSnapshot: snapshot.getChildren()){
ContentModel model = dataSnapshot.getValue(ContentModel.class);
list.add(model);
}
Collections.shuffle(list);
adapter = new ContentAdapter(getActivity(),list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}else{
Toast.makeText(getActivity(), "No data found", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
This is what the error shows
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.stdio9, PID: 24822 com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179) at com.google.firebase .database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) at com.google.ZB F12E1515C25C7D8C0352F1413AB9A15Z.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80) at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203) at com.example.stdio9.fragment.HomeFragment$1.onDataChange(HomeFragment.java:65) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at ZC31B3236 4CE19CA8FCD150A417ECCE58Z.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:233) at android.app.ActivityThread.main(ActivityThread.java:8068) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
This is the line which it takes me to
ContentModel model = dataSnapshot.getValue(ContentModel.class);
This is my JSON File
{
"Channels": {
"K3Q3vk5K0KYsXzikFR3ISRI97Np1": {
"channel_logo": "https://lh3.googleusercontent.com/a/AItbvmmoALgeo1I6QAmFiVRMZzErdjciiykyz8IA3l23=s96-c",
"channel_name": "Food insider",
"description": "Food Vlogs",
"joined": "4 Sep 2022",
"uid": "K3Q3vk5K0KYsXzikFR3ISRI97Np1"
},
"kNFLhugOP1e18pWvMWcdhNfeMjZ2": {
"channel_logo": "https://lh3.googleusercontent.com/a-/AFdZucqGchXowFwL77eSywfbyeOQbCsotcfoMr9f4cN2Kw=s96-c",
"channel_name": "Ryan Fernandes",
"description": "My Life through Vlogs ",
"joined": "Aug 19, 2022",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2"
}
},
"Content": {
"-NB16m0MkgzfJ55tkqsW": {
"date": "Sep 3, 2022",
"playlist": "Dark Place",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"type": "video",
"video_description": "Dark Place Juice Wrld - Unreleased",
"video_tags": "[999 LLJW]",
"video_title": "Unreleased Song by Juice Wrld",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Content%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662187923103%2Cmp4?alt=media&token=48437c1b-e21d-4f31-9240-b074b59182dd",
"videoid": "-NB16m0MkgzfJ55tkqsW",
"views": 0
},
"-NB7dlr2Sw-cmKHpy-0s": {
"date": "Sep 4, 2022",
"playlist": "999",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"type": "video",
"video_description": "Juice Wrld Dark Place - Unreleased",
"video_tags": "[999 LLJW]",
"video_title": "Unreleased Song by Juice Wrld\n999🖤",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Content%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662297498874%2Cmp4?alt=media&token=a7b262d6-8b63-4ff8-97ee-85a922b8ea71",
"videoid": "-NB7dlr2Sw-cmKHpy-0s",
"views": 0
},
"-NB7e7_dV8OMG3rzBZ47": {
"date": "Sep 4, 2022",
"playlist": "Juice wrld",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"type": "video",
"video_description": "Robbery - Juice Wrld",
"video_tags": "[999]",
"video_title": "Fl Studio",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Content%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662297573502%2Cwebm?alt=media&token=775dfcad-ad30-4d80-87bf-2b6c6e132057",
"videoid": "-NB7e7_dV8OMG3rzBZ47",
"views": 0
},
"-NBBNYPsjfsRbz4lpHd2": {
"date": "Sep 5, 2022",
"playlist": "Juice wrld",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"type": "video",
"video_description": "The Light",
"video_tags": "[999]",
"video_title": "Unreleased",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Content%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662360092449%2Cmp4?alt=media&token=4801e425-8179-4b73-98d7-73c0fa3f2ca9",
"videoid": "-NBBNYPsjfsRbz4lpHd2",
"views": 0
},
"-NBLMZqiW7xLZ9GPqzGk": {
"date": "Sep 7, 2022",
"playlist": "Juice wrld",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"type": "video",
"video_description": "123",
"video_tags": "[123]",
"video_title": "123",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Content%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662527602262%2Cmp4?alt=media&token=33baafb8-1551-4535-89ea-efaf3ba7f854",
"videoid": "-NBLMZqiW7xLZ9GPqzGk",
"views": 0
}
},
"Playlists": {
"kNFLhugOP1e18pWvMWcdhNfeMjZ2": {
"123": {
"playlist_name": "123",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 1
},
"999": {
"playlist_name": "999",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 1
},
"Dark Place": {
"playlist_name": "Dark Place",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 1
},
"Juice wrld": {
"playlist_name": "Juice wrld",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 3
},
"JuiceWrld": {
"playlist_name": "JuiceWrld",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 0
},
"JuiceWrld ": {
"playlist_name": "JuiceWrld ",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 0
},
"The Light ": {
"playlist_name": "The Light ",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"videos": 0
}
}
},
"Users": {
"K3Q3vk5K0KYsXzikFR3ISRI97Np1": {
"email": "b16sydelle@auxiliumwadala.net",
"profile": "https://lh3.googleusercontent.com/a/AItbvmmoALgeo1I6QAmFiVRMZzErdjciiykyz8IA3l23=s96-c",
"search": "sydelle fernandes",
"uid": "K3Q3vk5K0KYsXzikFR3ISRI97Np1",
"username": "SYDELLE FERNANDES"
},
"QEQmEdSOzXYbRKgdKxltlZUgiGJ2": {
"email": "fernandesryan1626@gmail.com",
"profile": "https://lh3.googleusercontent.com/a-/AFdZucqGchXowFwL77eSywfbyeOQbCsotcfoMr9f4cN2Kw=s96-c",
"search": "ryan fernandes",
"uid": "QEQmEdSOzXYbRKgdKxltlZUgiGJ2",
"username": "Ryan Fernandes"
},
"kNFLhugOP1e18pWvMWcdhNfeMjZ2": {
"email": "fernandesryan1626@gmail.com",
"profile": "https://lh3.googleusercontent.com/a-/AFdZucqGchXowFwL77eSywfbyeOQbCsotcfoMr9f4cN2Kw=s96-c",
"search": "ryan fernandes",
"uid": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"username": "Ryan Fernandes"
},
"lQxgXgOxmHdiDDpjmdZowTzX0qA2": {
"email": "fernandesebastian@yahoo.com",
"profile": "https://lh3.googleusercontent.com/a/AItbvmlIkNndyRu4z5X0NTHasNqglbNo8FOTUCrSegn1=s96-c",
"search": "seby fernandes",
"uid": "lQxgXgOxmHdiDDpjmdZowTzX0qA2",
"username": "Seby fernandes"
},
"rSWf85R1yxNqe49isLO1E2sJjmt1": {
"email": "gcs006.ryan.fernandes@gnkhalsa.edu.in",
"profile": "https://lh3.googleusercontent.com/a-/AFdZucqYM3lsrdxQcfnqef-q_tL6j2643EPnX0OxGrcd=s96-c",
"search": "ryan fernandes",
"uid": "rSWf85R1yxNqe49isLO1E2sJjmt1",
"username": "RYAN FERNANDES"
}
},
"Videos": {
"-NB13j1vAHj6-bTgYF1x": {
"date": "Sep 3, 2022",
"playlist": "123",
"publisher": "kNFLhugOP1e18pWvMWcdhNfeMjZ2",
"video_description": "Dance",
"video_tags": "[dance]",
"video_title": "The best ever",
"video_url": "https://firebasestorage.googleapis.com/v0/b/stdio9-501d8.appspot.com/o/Videos%2FkNFLhugOP1e18pWvMWcdhNfeMjZ2%2F1662187073009%2Cmp4?alt=media&token=55cd9f0d-d54b-41bd-bc74-5518b2f89828",
"videoid": "-NB13j1vAHj6-bTgYF1x"
}
}
}
My ContentModel java file
package com.example.stdio9.Models;
public class ContentModel {
String id,publisher,playlist,type,video,video_description,video_url,video_tags,views,video_title,date;
public ContentModel(String id, String publisher, String playlist, String type, String video, String video_description, String video_url, String video_tags, String views, String video_title, String date) {
this.id = id;
this.publisher = publisher;
this.playlist = playlist;
this.type = type;
this.video = video;
this.video_description = video_description;
this.video_url = video_url;
this.video_tags = video_tags;
this.views = views;
this.video_title = video_title;
this.date = date;
}
public ContentModel() {}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPlaylist() {
return playlist;
}
public void setPlaylist(String playlist) {
this.playlist = playlist;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public String getVideo_description() {
return video_description;
}
public void setVideo_description(String video_description) {
this.video_description = video_description;
}
public String getVideo_url() {
return video_url;
}
public void setVideo_url(String video_url) {
this.video_url = video_url;
}
public String getVideo_tags() {
return video_tags;
}
public void setVideo_tags(String video_tags) {
this.video_tags = video_tags;
}
public String getViews() {
return views;
}
public void setViews(String views) {
this.views = views;
}
public String getVideo_title() {
return video_title;
}
public void setVideo_title(String video_title) {
this.video_title = video_title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
While it's hard to be certain without seeing the JSON at Content
in your database, my guess is that you have a single item with its properties in the database. If that's the case, you shouldn't be looping over snapshot.getChildren()
but do:
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
list.clear();
ContentModel model = snapshot.getValue(ContentModel.class);
list.add(model);
Collections.shuffle(list);
adapter = new ContentAdapter(getActivity(),list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}else{
Toast.makeText(getActivity(), "No data found", Toast.LENGTH_SHORT).show();
}
}
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.