简体   繁体   中英

How to Insert the Enum Data in the Sqlite Database in Android?

I have an enum class like

public enum DutyState {

    DRIVING(0),
    ONDUTY(1),
    OFFDUTY(2),
    SLEEPER(3),
    NOSTATE(-1);
    private final  int state;

    private DutyState(int s) {
        this.state = s;
    }
}

And I am using Dto for State like

DutyState status;

 public DutyState getStatus() {
        return status;
    }

    public void setStatus(DutyState status) {
        this.status = status;
    }

And I am saving this state in the database (Sqlite)

Contentvalues cv=new ContentValues();
cv.put(STATUS,String.valueof(dto.getStatus()));

Up to here status is saved in the database.

When I retrieve the state from the database, I run into a problem.

My code is

dto.setStatus(c.getInt(c.getColumnIndex(TIME_STATUS)));

In my Sqlite I create a dataType for status is int

In dto its return type is void.

How can I retrieve the state from the database?

Use this to convert Enum to string

cv.put(STATUS, dto.getStatus().name());

and this to convert string back to enum

dto.setStatus(Enum.valueOf(DutyState.class, c.getString(c.getColumnIndex(TIME_STATUS))));

The problem is that in the database you store the names of the enum values ( DRIVING , ONDUTY , ...) and you try to retrieve the values based on the id .

My suggestion is to refactor your code a bit.

First of all, consider whether you really need the id property of the DutyState . Every enum value has an automatically generated ordinal value. These ordinals starts from zero, so they will not be the same as the id you have assigned to your values.

If you decide to store the ordinal numbers of your enums, you can easily get the enum value by accessing the protected automatically generated static member VALUES . Please note that the ordinal values will change, if you change the order of your enum values. So when I store enums by their ordinals, I never change the order, never delete an enum value and I only add enum values to the end.

Other option is to store in the database the name of the enum values. This is in general the safest option, (if you don't change the names of the enum values). This option is a bit less efficient, because it uses more space in the database. @Ridcully has already shown how to do it.

As a third option you can use the id as you have them now. This option is safe and efficient. You just need to change the way how you covert the stored id to the enum value.

You can do it this way:

public enum DutyState {

   DRIVING(0),
   ONDUTY(1),
   OFFDUTY(2),
   SLEEPER(3),
   NOSTATE(-1);
   private final  int state;

   private DutyState(int s) {
      this.state = s;
      ID_TO_STATE.put(s, this);
   }

   private static Map<Int, DutyState> ID_TO_STATE = new HashMap<>();
   public DutyState fromId(int s) {
      return ID_TO_STATE.get(s);
   }
}

Then the retrieval would be simply:

dto.setStatus(DutyState.fromId(c.getInt(TIME_STATUS)));

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