I have seen tons of example saving and retrieving ArrayList in Room, Android. But I can't find how to store the classical array [] objects as a field in Room. When I use a type Converter I get the errors:
"Error:(55, 18) error: Not sure how to convert a Cursor to this method's return type" "Error:(61, 38) error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this."
Here is the code:
Database file:
@Database(entities = {UserModel.class}, version = 4)
@TypeConverters({TutorsModelArrayConverter.class})
public abstract class AppDatabase extends RoomDatabase {
.... Database methods
}
Model File:
@Entity(primaryKeys = {"user_id"})
public class UserModel {
private String user_id;
private TutorModel[] tutors;
.... Other parameters
}
Converter file:
public class TutorsModelArrayConverter {
@TypeConverter
public static TutorModel[] toListTutor(String tutorModel) {
if (tutorModel == null) {
return null;
}
Gson gson = new Gson();
Type type = new TypeToken<TutorModel[]>() {}.getType();
return gson.fromJson(tutorModel, type);
}
@TypeConverter
public static String tutorModelToString(TutorModel[] tutorModels) {
if (tutorModels != null) {
return new Gson().toJson(tutorModels);
}else
return null;
}
}
DAO file:
@Dao
public interface DAO_DBHelper {
@Query("SELECT tutors FROM AppData WHERE fake_id = 1")
TutorModel[] getTutorList();
... Other methods
}
My question is how can I store an array [] of objects in Room and get around the Errors before mentioned. Any clue is welcome! Thanks in advance!