Room database architecture entity extends error - android

While using android Room i'm having the following entity:
#Entity
public class Call implements Parcelable {
#PrimaryKey(autoGenerate = true)
private long id;
private String filePath;
private long durationInMillis;
private String phoneNumber;
private int isStarred;
private int isIncoming;
private long timestampCreated;
}
All works great. But now I want my pojo (Call.class) to extends an Abstract class as following:
#Entity
public class Call extends BaseViewTypeData implements Parcelable {
....
....
}
And i'm getting the following error:
Error:Cannot figure out how to save this field into database. You can
consider adding a type converter for it.
Error:Cannot find getter for field.
Error:Cannot find setter for field.
Error:Cannot figure out how to read this field from a cursor.
Error:Cannot find getter for field.
Error:Cannot find setter for field.
The parent (BaseViewTypeData.class) is a simple class to handle multiple view types in a recycler views.
public abstract class BaseViewTypeData extends BaseObservable {
public static final int VIEW_TYPE_CALL = 0;
public static final int VIEW_TYPE_SETTINGS_HEADER = 1;
public static final int VIEW_TYPE_SETTINGS_TITLE_SUBTITLE = 2;
public static final int VIEW_TYPE_SETTINGS_TITLE_SUBTITLE_SWITCH = 3;
public static final int VIEW_TYPE_SETTINGS_DIVIDER = 4;
public static final int VIEW_TYPE_SETTINGS_TITLE_SWITCH = 5;
public static final int VIEW_TYPE_CALL_LOG_DATA = 6;
public static final int VIEW_TYPE_CHECKBOX_TITLE_SUBTITLE = 7;
#Ignore
public abstract int getViewType();
}

The parent (BaseViewTypeData.class) is a simple class to handle multiple view types in a recycler views.
I suspect that your problem is not with BaseViewTypeData, but with BaseObservable, as Room will not know how to deal with the BaseObservable fields.
In general, having your entity inherit from classes that you do not control is unlikely to work.

Related

Android ExpandableListview with Room database

I need to populate an ExpandableListView whose data is fetched from Room database.
There are answers on how to do this with SQLiteDatabase:
1) Android ExpandableListView and SQLite Database
2) Android ExpandableListView From Database
Is it possible to achieve the same with Room Database?
I have two tables: a) GroupHeader b) GroupContent
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
Any suggestions please?
Found the solution using #Relation.
Reference: https://developer.android.com/reference/android/arch/persistence/room/Relation
#Entity
public class GroupHeader implements Serializable {
#PrimaryKey(autoGenerate = true)
private int groupId;
private String groupName;
private String otherProperty1;
private String otherProperty2;
/* getters and setters */
}
#Entity
public class GroupContent implements Serializable {
#PrimaryKey(autoGenerate = true)
private int contentId;
private int groupId;
private String contentName;
private String otherProperty3;
private String otherProperty4;
/* getters and setters */
}
public class Wrapper {
#Embedded
private GroupHeader header;
#Relation(parentColumn="groupId", entityColumn="groupId", entity=GroupContent.class)
private List<GroupContent> contents;
/*getters & setters*/
}

Why we pass super((String)IntentServiceClassName ) in constructor while extending IntentService class in Android

Why we pass super((String)IntentServiceClassName ) in constructor while extending IntentService class in Android.
Why we use that construtor empty. and why with name >??
public class MyService extends IntentService {
public static final int STATUS_RUNNING = 0;
public static final int STATUS_FINISHED = 1;
public static final int STATUS_ERROR = 2;
private static final String TAG = "MyService";
public MyService() {
super(MyService.class.getName());
//or super("MyService");
}
Because we have to instantiate the constructor of the parent class too to notify it that its child class has been created...
In java if you do not provide the super as the first statement the compiler automatically attach this statement as the first line after compilation...

Why are some constants public and other private in an Android library code

Problem
Why some constants are under the public modifier while some other private? Are those under public can be called from applications that use the library? If so, how to call the constant from an app, is it like this: CertainLibraryClass.ActivityResultCode.CODE_A?
Code
public class CertainLibraryClass {
public class ActivityResultCode {
public static final int CODE_A = 0X02;
public static final int CODE_B = 0X03;
public static final int CODE_C = 0X04;
}
public class VersionCode {
private static final int VERSION_MAJOR = 1;
private static final int VERSION_MINOR1 = 0;
private static final int VERSION_MINOR2 = 2;
}
// ....
}
Why some constants are under the public modifier?
Ans: So that all other classes can access it e.g.RESULT_OK,SUCCESS.
Why some constants are under the private modifier?
Ans:So that only that class can access it
e.g. consider you are calling getId() libarary function from your class
public class CertainLibraryClass {
private static int ID=0;
public static int getId()
{
return ID+1;
}
here you are not accessing ID field directly ,instead you are calling getId() function which ultimately returns the id, it means that ID variable is internally used by CertainLibraryClass class

How can I "map" in ORMLite a base object that all Database entities extend

I have an object that I call BaseObject which all classes that are persisted extend. This class has an _id field, dateCreated, dateModified, etc.... The database tables that correspond to all objects that extends this base object obviously have columns that store these values.
How can I set up these fields using annotations so all these fields are persisted to the database? Can I do this or will I need to add these fields to the POJOs directly? I have included below my base object and one of the classes that extends it.
public class BaseObject {
private int _id;
private String dateCreated;
private String dateModified;
private String createModule;
private String modifiedModule;
// getters and setters here ...
}
Here's the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
public Food(int foodGroupId, String longDescription) {
this.foodGroupId = foodGroupId;
this.longDescription = longDescription;
}
private Integer foodGroupId;
private String longDescription;
// getters and setters here ...
}
Any help would be appreciated. Thank you.
This should work well with ORMLite as long as you annotate each of the fields in both the base-class and the super-class that you want persisted. For example, you need to need to add something like the following annotations to the BaseObject.
#DatabaseField(generatedId = true)
private int _id;
#DatabaseField
private String dateCreated;
#DatabaseField
private String dateModified;
#DatabaseField
private String createModule;
#DatabaseField
private String modifiedModule;
And then you add the following annotations to the super-class:
#DatabaseTable(tableName = "FOOD")
public class Food extends BaseObject {
#DatabaseField
private Integer foodGroupId;
#DatabaseField
private String longDescription;

R.java can't be modified

I was trying to modify the R.java in android. I deleted the generated java files. After saving, it doesn't seem to change anything. The R.java file i have looks like this
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int myEditText=0x7f050000;
public static final int myListView=0x7f050001;
}
public static final class layout {
public static final int colors=0x7f030000;
public static final int dimens=0x7f030001;
public static final int main=0x7f030002;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
I wanted to change it, to be more like this one:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
public final class R {
public static final class attr {
}
public static final class color {
public static final int notepad_lines=0x7f040001;
public static final int notepad_margin=0x7f040002;
public static final int notepad_paper=0x7f040000;
public static final int notepad_text=0x7f040003;
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int myEditText=0x7f050000;
public static final int myListView=0x7f050001;
}
public static final class layout {
public static final int colors=0x7f030000;
public static final int dimens=0x7f030001;
public static final int main=0x7f030002;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
It so frustrating every time I save it, it goes back to its old state because it's auto generated . How do I modify it?
R.java is an auto generated file contains all your resources used in project. If you want to change R.java you can't.
You have to add or delete resources, then it will be modified according to your resources present in project.
This is basic thing in Android. You have to read Android Developers documents, then you will get good knowledge about it.
You cannot modify R.java in android it will be generated automatically when we are developing an xml file.... here id numbers will be stored of different views so we can't modify it..

Categories

Resources