Firebase数据加载至RecyclerView时遇String转int类型转换错误
Hey there, let's break down and fix this conversion error you're hitting when loading Firebase data into your RecyclerView.
What's Causing the Error?
The core issue jumps right out from the first line of your stack trace:
Failed to convert a value of type java.lang.String to int
This means one (or both) of the int fields in your StreetDataFetcher class (Likes or Views) is trying to map to a string value in your Firebase Realtime Database. For example, Firebase might have "50" (a quoted string) stored for Likes, but your code expects a raw integer 50.
Step-by-Step Fixes
1. Verify Firebase Database Data Types
Head to your Firebase Console, navigate to your Realtime Database, and check the values for Likes and Views:
- If the values are wrapped in quotes (e.g.,
"123"), they're stored as strings. You'll need to update them to raw numbers (e.g.,123) to match yourintfields. - If you can't modify the database structure (e.g., the backend intentionally stores numbers as strings), skip to step 2.
2. Adjust Your StreetDataFetcher Class
Based on your database's actual data type, update your entity class accordingly:
Option A: Keep fields as int (if database uses numbers)
Make sure your class has a complete constructor, getter methods, and a public no-argument constructor (required by Firebase for deserialization). Here's a corrected, full version:
public class StreetDataFetcher { private String UserName; private String Long_Desc; private String Short_Desc; private String DateTime; private String VideoName; private String VideoPath; private String ProfileImage; private int Likes; private int Views; // Required no-argument constructor for Firebase deserialization public StreetDataFetcher() {} // Full parameterized constructor public StreetDataFetcher(String userName, String long_Desc, String short_Desc, String dateTime, String videoName, String videoPath, String profileImage, int likes, int views) { UserName = userName; Long_Desc = long_Desc; Short_Desc = short_Desc; DateTime = dateTime; VideoName = videoName; VideoPath = videoPath; ProfileImage = profileImage; Likes = likes; Views = views; } // Getter methods for all fields public String getUserName() { return UserName; } public String getLong_Desc() { return Long_Desc; } public String getShort_Desc() { return Short_Desc; } public String getDateTime() { return DateTime; } public String getVideoName() { return VideoName; } public String getVideoPath() { return VideoPath; } public String getProfileImage() { return ProfileImage; } public int getLikes() { return Likes; } public int getViews() { return Views; } }
Option B: Change fields to String (if database uses string-formatted numbers)
If you can't modify the database, update Likes and Views to String types, then convert them to integers when displaying in your RecyclerView:
public class StreetDataFetcher { // ... other fields remain the same ... private String Likes; private String Views; // Update constructor and getters accordingly public StreetDataFetcher(String userName, String long_Desc, String short_Desc, String dateTime, String videoName, String videoPath, String profileImage, String likes, String views) { // ... initialize other fields ... Likes = likes; Views = views; } public String getLikes() { return Likes; } public String getViews() { return Views; } }
Then in your onBindViewHolder method, convert the strings to integers when setting UI values:
@Override protected void onBindViewHolder(@NonNull YourViewHolder holder, int position, @NonNull StreetDataFetcher model) { // Convert string values to integers for display int likesCount = Integer.parseInt(model.getLikes()); int viewsCount = Integer.parseInt(model.getViews()); holder.likesTextView.setText(String.valueOf(likesCount)); holder.viewsTextView.setText(String.valueOf(viewsCount)); // ... other UI bindings ... }
3. Double-Check Field Name Matching
Ensure the field names in your StreetDataFetcher class exactly match the keys in Firebase (case-sensitive!). For example, if Firebase uses likes (lowercase), your class should have private int likes; (not Likes). If they don't match, use the @PropertyName annotation to map them explicitly:
@PropertyName("likes") private int Likes; @PropertyName("likes") public int getLikes() { return Likes; }
Final Notes
After making these changes, restart your app and test again. The conversion error should disappear once your entity class fields align with the data types and keys stored in Firebase.
内容的提问来源于stack exchange,提问作者Mohit Gupta

